2.6 Logicals
Related Reading: IDS 2.13
All programming languages have ways of tracking whether variables meet certain criteria. These are often called Booleans or Logicals. For us, this will particularly come up in the context of subsetting data (i.e., selecting data based on some condition) and in running particular portions of code based on some condition.
Some main logical operators are ==
, <=
, >=
, <
, >
corresponding to whether or not two things are equal, less than or equal to, greater than or equal, strictly less than, and strictly greater than. These can be applied to vectors. And the comparisons result in either TRUE
or FALSE
. Here are some examples
<- c(1,2,3,4,5)
five
# only 3 is equal to 3
== 3
five #> [1] FALSE FALSE TRUE FALSE FALSE
# 1,2,3 are all less than or equal to 3
<= 3
five #> [1] TRUE TRUE TRUE FALSE FALSE
# 3,4,5, are all greater than or equal to 3
>= 3
five #> [1] FALSE FALSE TRUE TRUE TRUE
# 1,2 are strictly less than 3
< 3
five #> [1] TRUE TRUE FALSE FALSE FALSE
# 4,5 are strictly greater than 3
> 3
five #> [1] FALSE FALSE FALSE TRUE TRUE
Example 2.3 Often, we might be interested in learning about a subset of our data. As a simple example, using our firm_data
from earlier, you could imagine being interested in average employment for manufacturing firms.
We can do this using the subset
function along with the logical operations we’ve learned in this section.
<- subset(firm_data, industry=="Manufacturing")
manufacturing_firms mean(manufacturing_firms$employees)
#> [1] 252.3333
As practice, try creating a subset of firm_data
based on firms having more than 100 employees.
2.6.1 Additional Logical Operators
Related Reading: IDS 2.13
There are a number of additional logical operators that can be useful in practice. Here, we quickly cover several more.
!=
— not equalc(1,2,3) != 3 #> [1] TRUE TRUE FALSE
We can link together multiple logical comparisons. If we want to check whether multiple conditions hold, we can use “logical AND”
&
; if we want to check whether any of multiple conditions hold, we can use “logical OR”|
.# AND c(1,2,3,4,5) >= 3 ) & ( c(1,2,3,4,5) < 5 ) ( #> [1] FALSE FALSE TRUE TRUE FALSE # OR c(1,2,3,4,5) >= 4 ) | ( c(1,2,3,4,5) < 2 ) ( #> [1] TRUE FALSE FALSE TRUE TRUE
%in%
— checks whether the elements of one vector show up in another vector# 1 is in the 2nd vector, but 7 is not c(1,7) %in% c(1,2,3,4,5) #> [1] TRUE FALSE
Often it is useful to check whether any logical conditions are true or all logical conditions are true. This can be done as follows
# this one is TRUE because 1 is in the 2nd vector any(c(1,7) %in% c(1,2,3,4,5)) #> [1] TRUE # this one is FALSE because 7 is not in the 2nd vector all(c(1,7) %in% c(1,2,3,4,5)) #> [1] FALSE