14 Logical vectors
You are reading the work-in-progress second edition of R for Data Science. This chapter should be readable but is currently undergoing final polishing. You can find the complete first edition at https://r4ds.had.co.nz.
14.1 Introduction
In this chapter, you’ll learn tools for working with logical vectors. Logical vectors are the simplest type of vector because each element can only be one of three possible values: TRUE
, FALSE
, and NA
. It’s relatively rare to find logical vectors in your raw data, but you’ll create and manipulate in the course of almost every analysis.
We’ll begin by discussing the most common way of creating logical vectors: with numeric comparisons. Then you’ll learn about how you can use use Boolean algebra to combine different logical vectors, as well some useful summaries. We’ll finish off with some tools for making conditional changes, and a cool hack for turning logical vectors into groups.
14.1.1 Prerequisites
Most of the functions you’ll learn about in this chapter are provided by base R, so we don’t need the tidyverse, but but we’ll still load it so we can use mutate()
, filter()
, and friends to work with data frames. We’ll also continue to draw examples from the nyclights13 dataset.
However, as we start to cover more tools, there won’t always be a perfect real example. So we’ll start making up some dummy data with c()
:
x <- c(1, 2, 3, 5, 7, 11, 13)
x * 2
#> [1] 2 4 6 10 14 22 26
This makes it easier to explain individual functions at the cost to making it harder to see how it might apply to your data problems. Just remember that any manipulation we do to a free-floating vector, you can do to a variable inside data frame with mutate()
and friends.
14.2 Comparisons
A very common way to create a logical vector is via a numeric comparison with <
, <=
, >
, >=
, !=
, and ==
. So far, we’ve mostly create logical variables transiently within filter()
— they are computed, used, and then throw away. For example, the following filter finds all daytime departures that leave roughly on time:
flights |>
filter(dep_time > 600 & dep_time < 2000 & abs(arr_delay) < 20)
#> # A tibble: 172,286 × 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 1 1 601 600 1 844 850
#> 2 2013 1 1 602 610 -8 812 820
#> 3 2013 1 1 602 605 -3 821 805
#> 4 2013 1 1 606 610 -4 858 910
#> 5 2013 1 1 606 610 -4 837 845
#> 6 2013 1 1 607 607 0 858 915
#> # … with 172,280 more rows, and 11 more variables: arr_delay <dbl>,
#> # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
#> # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
It’s useful to know that this is a shortcut and you can explicitly create the underlying logical variables with mutate()
:
flights |>
mutate(
daytime = dep_time > 600 & dep_time < 2000,
approx_ontime = abs(arr_delay) < 20,
.keep = "used"
)
#> # A tibble: 336,776 × 4
#> dep_time arr_delay daytime approx_ontime
#> <int> <dbl> <lgl> <lgl>
#> 1 517 11 FALSE TRUE
#> 2 533 20 FALSE FALSE
#> 3 542 33 FALSE FALSE
#> 4 544 -18 FALSE TRUE
#> 5 554 -25 FALSE FALSE
#> 6 554 12 FALSE TRUE
#> # … with 336,770 more rows
This is particularly useful for more complicated logic because naming the intermediate steps makes it easier to both read your code and check that each step has been computed correctly.
All up, the initial filter is equivalent to:
14.2.1 Floating point comparison
Beware of using ==
with numbers. For example, it looks like this vector contains the numbers 1 and 2:
But if you test them for equality, you get FALSE
:
x == c(1, 2)
#> [1] FALSE FALSE
What’s going on? Computers store numbers with a fixed number of decimal places so there’s no way to exactly represent 1/49 or sqrt(2)
and subsequent computations will be very slightly off. We can see the exact values by calling print()
with the the digits
1 argument:
print(x, digits = 16)
#> [1] 0.9999999999999999 2.0000000000000004
You can see why R defaults to rounding these numbers; they really are very close to what you expect.
Now that you’ve seen why ==
is failing, what can you do about it? One option is to use dplyr::near()
which ignores small differences:
14.2.2 Missing values
Missing values represent the unknown so they are “contagious”: almost any operation involving an unknown value will also be unknown:
NA > 5
#> [1] NA
10 == NA
#> [1] NA
The most confusing result is this one:
NA == NA
#> [1] NA
It’s easiest to understand why this is true if we artificial supply a little more context:
# Let x be Mary's age. We don't know how old she is.
x <- NA
# Let y be John's age. We don't know how old he is.
y <- NA
# Are John and Mary the same age?
x == y
#> [1] NA
# We don't know!
So if you want to find all flights with dep_time
is missing, the following code doesn’t work because dep_time == NA
will yield a NA
for every single row, and filter()
automatically drops missing values:
flights |>
filter(dep_time == NA)
#> # A tibble: 0 × 19
#> # … with 19 variables: year <int>, month <int>, day <int>, dep_time <int>,
#> # sched_dep_time <int>, dep_delay <dbl>, arr_time <int>,
#> # sched_arr_time <int>, arr_delay <dbl>, carrier <chr>, flight <int>,
#> # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
#> # hour <dbl>, minute <dbl>, time_hour <dttm>
Instead we’ll need a new tool: is.na()
.
14.2.3 is.na()
is.na(x)
works with any type of vector and returns TRUE
for missing values and FALSE
for everything else:
We can use is.na()
to find all the rows with a missing dep_time
:
flights |>
filter(is.na(dep_time))
#> # A tibble: 8,255 × 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 1 1 NA 1630 NA NA 1815
#> 2 2013 1 1 NA 1935 NA NA 2240
#> 3 2013 1 1 NA 1500 NA NA 1825
#> 4 2013 1 1 NA 600 NA NA 901
#> 5 2013 1 2 NA 1540 NA NA 1747
#> 6 2013 1 2 NA 1620 NA NA 1746
#> # … with 8,249 more rows, and 11 more variables: arr_delay <dbl>,
#> # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
#> # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
is.na()
can also be useful in arrange()
. arrange()
usually puts all the missing values at the end but you can override this default by first sorting by is.na()
:
flights |>
filter(month == 1, day == 1) |>
arrange(dep_time)
#> # A tibble: 842 × 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 1 1 517 515 2 830 819
#> 2 2013 1 1 533 529 4 850 830
#> 3 2013 1 1 542 540 2 923 850
#> 4 2013 1 1 544 545 -1 1004 1022
#> 5 2013 1 1 554 600 -6 812 837
#> 6 2013 1 1 554 558 -4 740 728
#> # … with 836 more rows, and 11 more variables: arr_delay <dbl>, carrier <chr>,
#> # flight <int>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
#> # distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
flights |>
filter(month == 1, day == 1) |>
arrange(desc(is.na(dep_time)), dep_time)
#> # A tibble: 842 × 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 1 1 NA 1630 NA NA 1815
#> 2 2013 1 1 NA 1935 NA NA 2240
#> 3 2013 1 1 NA 1500 NA NA 1825
#> 4 2013 1 1 NA 600 NA NA 901
#> 5 2013 1 1 517 515 2 830 819
#> 6 2013 1 1 533 529 4 850 830
#> # … with 836 more rows, and 11 more variables: arr_delay <dbl>, carrier <chr>,
#> # flight <int>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
#> # distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
14.2.4 Exercises
- How does
dplyr::near()
work? Typenear
to see the source code. - Use
mutate()
,is.na()
, andcount()
together to describe how the missing values indep_time
,sched_dep_time
anddep_delay
are connected.
14.3 Boolean algebra
Once you have multiple logical vectors, you can combine them together using Boolean algebra. In R, &
is “and”, |
is “or”, and !
is “not”, and xor()
is exclusive or2. Figure 14.1 shows the complete set of Boolean operations and how they work.
As well as &
and |
, R also has &&
and ||
. Don’t use them in dplyr functions! These are called short-circuiting operators and only ever return a single TRUE
or FALSE
. They’re important for programming and you’ll learn more about them in Section 29.4.
14.3.1 Missing values
The rules for missing values in Boolean algebra are a little tricky to explain because they seem inconsistent at first glance:
To understand what’s going on, think about NA | TRUE
. A missing value in a logical vector means that the value could either be TRUE
or FALSE
. TRUE | TRUE
and FALSE | TRUE
are both TRUE
, so NA | TRUE
must also be TRUE
. Similar reasoning applies with NA & FALSE
.
14.3.2 Order of operations
Note that the order of operations doesn’t work like English. Take the following code finds all flights that departed in November or December:
flights |>
filter(month == 11 | month == 12)
You might be tempted to write it like you’d say in English: “find all flights that departed in November or December”:
flights |>
filter(month == 11 | 12)
#> # A tibble: 336,776 × 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 1 1 517 515 2 830 819
#> 2 2013 1 1 533 529 4 850 830
#> 3 2013 1 1 542 540 2 923 850
#> 4 2013 1 1 544 545 -1 1004 1022
#> 5 2013 1 1 554 600 -6 812 837
#> 6 2013 1 1 554 558 -4 740 728
#> # … with 336,770 more rows, and 11 more variables: arr_delay <dbl>,
#> # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
#> # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
This code doesn’t error but it also doesn’t seem to have worked. What’s going on? Here R first evaluates month == 11
creating a logical vector, which I’ll call nov
. It computes nov | 12
. When you use a number with a logical operator it converts everything apart from 0 to TRUE, so this is equivalent to nov | TRUE
which will always be TRUE
, so every row will be selected:
flights |>
mutate(
nov = month == 11,
final = nov | 12,
.keep = "used"
)
#> # A tibble: 336,776 × 3
#> month nov final
#> <int> <lgl> <lgl>
#> 1 1 FALSE TRUE
#> 2 1 FALSE TRUE
#> 3 1 FALSE TRUE
#> 4 1 FALSE TRUE
#> 5 1 FALSE TRUE
#> 6 1 FALSE TRUE
#> # … with 336,770 more rows
14.3.3 %in%
An easy way to avoid the problem of getting your ==
s and |
s in the right order is to use %in%
. x %in% y
returns a logical vector the same length as x
that is TRUE
whenever a value in x
is anywhere in y
.
So to find all flights in November and December we could write:
Note that %in%
obeys different rules for NA
to ==
, as NA %in% NA
is TRUE
.
This can make for a useful shortcut:
flights |>
filter(dep_time %in% c(NA, 0800))
#> # A tibble: 8,803 × 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 1 1 800 800 0 1022 1014
#> 2 2013 1 1 800 810 -10 949 955
#> 3 2013 1 1 NA 1630 NA NA 1815
#> 4 2013 1 1 NA 1935 NA NA 2240
#> 5 2013 1 1 NA 1500 NA NA 1825
#> 6 2013 1 1 NA 600 NA NA 901
#> # … with 8,797 more rows, and 11 more variables: arr_delay <dbl>,
#> # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
#> # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
14.3.4 Exercises
- Find all flights where
arr_delay
is missing butdep_delay
is not. Find all flights where neitherarr_time
norsched_arr_time
are missing, butarr_delay
is. - How many flights have a missing
dep_time
? What other variables are missing in these rows? What might these rows represent? - Assuming that a missing
dep_time
implies that a flight is cancelled, look at the number of cancelled flights per day. Is there a pattern? Is there a connection between the proportion of cancelled flights and average delay of non-cancelled flights?
14.4 Summaries
The following sections describe some useful techniques for summarizing logical vectors. As well as functions that only work specifically with logical vectors, you can also use functions that work with numeric vectors.
14.4.1 Logical summaries
There are two main logical summaries: any()
and all()
. any(x)
is the equivalent of |
; it’ll return TRUE
if there are any TRUE
’s in x
. all(x)
is equivalent of &
; it’ll return TRUE
only if all values of x
are TRUE
’s. Like all summary functions, they’ll return NA
if there are any missing values present, and as usual you can make the missing values go away with na.rm = TRUE
.
For example, we could use all()
to find out if there were days where every flight was delayed:
flights |>
group_by(year, month, day) |>
summarise(
all_delayed = all(arr_delay >= 0, na.rm = TRUE),
any_delayed = any(arr_delay >= 0, na.rm = TRUE),
.groups = "drop"
)
#> # A tibble: 365 × 5
#> year month day all_delayed any_delayed
#> <int> <int> <int> <lgl> <lgl>
#> 1 2013 1 1 FALSE TRUE
#> 2 2013 1 2 FALSE TRUE
#> 3 2013 1 3 FALSE TRUE
#> 4 2013 1 4 FALSE TRUE
#> 5 2013 1 5 FALSE TRUE
#> 6 2013 1 6 FALSE TRUE
#> # … with 359 more rows
In most cases, however, any()
and all()
are a little too crude, and it would be nice to be able to get a little more detail about how many values are TRUE
or FALSE
. That leads us to the numeric summaries.
14.4.2 Numeric summaries
When you use a logical vector in a numeric context, TRUE
becomes 1 and FALSE
becomes 0. This makes sum()
and mean()
very useful with logical vectors because sum(x)
will give the number of TRUE
s and mean(x)
the proportion of TRUE
s. That lets us see the distribution of delays across the days of the year as shown in Figure 14.2.
flights |>
group_by(year, month, day) |>
summarise(
prop_delayed = mean(arr_delay > 0, na.rm = TRUE),
.groups = "drop"
) |>
ggplot(aes(prop_delayed)) +
geom_histogram(binwidth = 0.05)
Or we could ask how many flights left before 5am, which are often flights that were delayed from the previous day:
flights |>
group_by(year, month, day) |>
summarise(
n_early = sum(dep_time < 500, na.rm = TRUE),
.groups = "drop"
) |>
arrange(desc(n_early))
#> # A tibble: 365 × 4
#> year month day n_early
#> <int> <int> <int> <int>
#> 1 2013 6 28 32
#> 2 2013 4 10 30
#> 3 2013 7 28 30
#> 4 2013 3 18 29
#> 5 2013 7 7 29
#> 6 2013 7 10 29
#> # … with 359 more rows
14.4.3 Logical subsetting
There’s one final use for logical vectors in summaries: you can use a logical vector to filter a single variable to a subset of interest. This makes use of the base [
(pronounced subset) operator, which you’ll learn more about this in Section 30.4.5.
Imagine we wanted to look at the average delay just for flights that were actually delayed. One way to do so would be to first filter the flights:
flights |>
filter(arr_delay > 0) |>
group_by(year, month, day) |>
summarise(
ahead = mean(arr_delay),
n = n(),
.groups = "drop"
)
#> # A tibble: 365 × 5
#> year month day ahead n
#> <int> <int> <int> <dbl> <int>
#> 1 2013 1 1 32.5 461
#> 2 2013 1 2 32.0 535
#> 3 2013 1 3 27.7 460
#> 4 2013 1 4 28.3 297
#> 5 2013 1 5 22.6 238
#> 6 2013 1 6 24.4 381
#> # … with 359 more rows
This works, but what if we wanted to also compute the average delay for flights that left early? We’d need to perform a separate filter step, and then figure out how to combine the two data frames together3. Instead you could use [
to perform an inline filtering: arr_delay[arr_delay > 0]
will yield only the positive arrival delays.
This leads to:
flights |>
group_by(year, month, day) |>
summarise(
ahead = mean(arr_delay[arr_delay > 0], na.rm = TRUE),
behind = mean(arr_delay[arr_delay < 0], na.rm = TRUE),
n = n(),
.groups = "drop"
)
#> # A tibble: 365 × 6
#> year month day ahead behind n
#> <int> <int> <int> <dbl> <dbl> <int>
#> 1 2013 1 1 32.5 -12.5 842
#> 2 2013 1 2 32.0 -14.3 943
#> 3 2013 1 3 27.7 -18.2 914
#> 4 2013 1 4 28.3 -17.0 915
#> 5 2013 1 5 22.6 -14.0 720
#> 6 2013 1 6 24.4 -13.6 832
#> # … with 359 more rows
Also note the difference in the group size: in the first chunk n()
gives the number of delayed flights per day; in the second, n()
gives the total number of flights.
14.4.4 Exercises
- What will
sum(is.na(x))
tell you? How aboutmean(is.na(x))
? - What does
prod()
return when applied to a logical vector? What logical summary function is it equivalent to? What doesmin()
return applied to a logical vector? What logical summary function is it equivalent to? Read the documentation and perform a few experiments.
14.5 Conditional transformations
One of the most powerful features of logical vectors are their use for conditional transformations, i.e. doing one thing for condition x, and something different for condition y. There are two important tools for this: if_else()
and case_when()
.
14.5.1 if_else()
If you want to use one value when a condition is true and another value when it’s FALSE
, you can use dplyr::if_else()
4. You’ll always use the first three argument of if_else(
). The first argument, condition
, is a logical vector, the second, true
, gives the output when the condition is true, and the third, false
, gives the output if the condition is false.
Let’s begin with a simple example of labeling a numeric vector as either “+ve” or “-ve”:
There’s an optional fourth argument, missing
which will be used if the input is NA
:
if_else(x > 0, "+ve", "-ve", "???")
#> [1] "-ve" "-ve" "-ve" "-ve" "+ve" "+ve" "+ve" "???"
You can also use vectors for the the true
and false
arguments. For example, this allows us to create a minimal implementation of abs()
:
if_else(x < 0, -x, x)
#> [1] 3 2 1 0 1 2 3 NA
So far all the arguments have used the same vectors, but you can of course mix and match. For example, you could implement a simple version of coalesce()
like this:
You might have noticed a small infelicity in our labeling: zero is neither positive nor negative. We could resolves this by adding an additional if_else():
This is already a little hard to read, and you can imagine it would only get harder if you have more conditions. Instead, you can switch to dplyr::case_when()
.
14.5.2 case_when()
dplyr’s case_when()
is inspired by SQL’s CASE
statement and provides a flexible way of performing different computations for different computations. It has a special syntax that unfortunately looks like nothing else you’ll use in the tidyverse. it takes pairs that look like condition ~ output
. condition
must be a logical vector; when it’s TRUE
, output
will be used.
This means we could recreate our previous nested if_else()
as follows:
This is more code, but it’s also more explicit.
To explain how case_when()
works, lets explore some simpler cases. If none of the cases match, the output gets an NA
:
case_when(
x < 0 ~ "-ve",
x > 0 ~ "+ve"
)
#> [1] "-ve" "-ve" "-ve" NA "+ve" "+ve" "+ve" NA
If you want to create a “default”/catch all value, use TRUE
on the left hand side:
case_when(
x < 0 ~ "-ve",
x > 0 ~ "+ve",
TRUE ~ "???"
)
#> [1] "-ve" "-ve" "-ve" "???" "+ve" "+ve" "+ve" "???"
And note that if multiple conditions match, only the first will be used:
case_when(
x > 0 ~ "-ve",
x > 3 ~ "big"
)
#> [1] NA NA NA NA "-ve" "-ve" "-ve" NA
Just like with if_else()
you can use variables on both sides of the ~
and you can mix and match variables as needed for your problem. For example, we could use case_when()
to provide some human readable labels for the arrival delay:
flights |>
mutate(
status = case_when(
is.na(arr_delay) ~ "cancelled",
arr_delay > 60 ~ "very late",
arr_delay > 15 ~ "late",
abs(arr_delay) <= 15 ~ "on time",
arr_delay < -15 ~ "early",
arr_delay < -30 ~ "very early",
),
.keep = "used"
)
#> # A tibble: 336,776 × 2
#> arr_delay status
#> <dbl> <chr>
#> 1 11 on time
#> 2 20 late
#> 3 33 late
#> 4 -18 early
#> 5 -25 early
#> 6 12 on time
#> # … with 336,770 more rows
14.6 Making groups
Before we move on to the next chapter, I want to show you one last trick. I don’t know exactly how to describe it, and it feels a little magical, but it’s super handy so I wanted to make sure you knew about it. Sometimes you want to divide your dataset up into groups based on the occurrence of some event. For example, when you’re looking at website data it’s common to want to break up events into sessions, where a session is defined an a gap of more than x minutes since the last activity.
Here’s some made up data that illustrates the problem. I’ve computed the time lag between the events, and figured out if there’s a gap that’s big enough to qualify.
events <- tibble(
time = c(0, 1, 2, 3, 5, 10, 12, 15, 17, 19, 20, 27, 28, 30)
)
events <- events |>
mutate(
diff = time - lag(time, default = first(time)),
gap = diff >= 5
)
events
#> # A tibble: 14 × 3
#> time diff gap
#> <dbl> <dbl> <lgl>
#> 1 0 0 FALSE
#> 2 1 1 FALSE
#> 3 2 1 FALSE
#> 4 3 1 FALSE
#> 5 5 2 FALSE
#> 6 10 5 TRUE
#> # … with 8 more rows
How do I go from that logical vector to something that I can group_by()
? You can use the cumulative sum, cumsum(),
to turn this logical vector into a unique group identifier. Remember that whenever you use a logical vector in a numeric context TRUE
becomes 1 and FALSE
becomes 0, taking the cumulative sum of a logical vector creates a numeric index that increments every time it sees a TRUE
.
R normally calls print for you (i.e.
x
is a shortcut forprint(x)
), but calling it explicitly is useful if you want to provide other arguments.↩︎That is,
xor(x, y)
is true if x is true, or y is true, but not both. This is how we usually use “or” In English. Both is not usually an acceptable answer to the question “would you like ice cream or cake?”.↩︎We’ll cover this in Chapter Chapter 13↩︎
dplyr’s
if_else()
is very similar to base R’sifelse()
. There are two main advantages ofif_else()
overifelse()
: you can choose what should happen to missing values, andif_else()
is much more likely to give you a meaningful error if you variables have incompatible types.↩︎