2.4 More on Logical Vectors
Consider the following expression:
13 < 20
## [1] TRUE
We constructed it with the “less-than” operator <
. You can think of it as saying that 13 is less than 20, which is a true statement, and sure enough, R evaluates the expression 13 < 20
as TRUE
.
When you think about it, we’ve seen lots of expressions so far. Here are just a few of them:
sqrt(64)
heights
heights[1:3]
13 < 20
When we type any one of them into the console, it evaluates to a particular value. In the examples above, the value was always a vector.
Expressions like 13 < 20
that evaluate to a logical vector are often called Boolean expressions.2
2.4.1 Boolean Operators
Let’s look further into Boolean expressions. Define the following two vectors:
<- c(10, 13, 17)
a <- c(8, 15, 12) b
Now let’s evaluate the expression a < b
:
< b a
## [1] FALSE TRUE FALSE
The <
operator, when applied to vectors, always works element-wise; that is, it is applied to corresponding elements of the vectors on either side of it. R’s evaluation of a < b
involves evaluation of the following three expressions:
10 < 8
(evaluates toFALSE
)13 < 15
(evaluates toTRUE
)17 < 12
(evaluates toFALSE
)
The result is a logical vector of length 3.
The <
operator is an example of a Boolean operator in R. Table 2.4.1 shows the available Boolean operators.
Operation | What It Means |
---|---|
< | less than |
> | greater than |
<= | less than or equal to |
>= | greater than or equal to |
== | equal to |
& | and |
| | or |
&& | and (scalar version) |
|| | or (scalar version) |
! | not |
2.4.1.1 Inequalities
The “numerical-looking operators” (<
, <=
, >
, >=
) have their usual meanings when one is working with numerical vectors3 When applied to character vectors they evaluate according to an alphabetical order:
<- c("Dorothy", "toto", "Boq")
a<- c("tinman", "Toto", "2017")
b < b a
## [1] TRUE TRUE FALSE
The reasons for the evaluation above are as follows:
- D comes before t in the alphabet;
- lowercase t comes before uppercase T, according to R;
- characters for numbers come before letter-characters, according to R.
2.4.1.2 Equality
The equality (==
) operator indicates whether the expressions being compared evaluate to the same value. Note that it’s made with two equal-signs, not one! It’s all about evaluation to the same value, not strict identity. The following examples will help to clarify this.
<- c(Dorothy = 1,Toto = 2) # a named vector
a <- c(Glinda = 1, Tinman = 2)
b == b a
## Dorothy Toto
## TRUE TRUE
(Note that the resulting logical vector inherits the names of a
, the vector on the left.).
But a
and b
aren’t identical. We can see this because R has the function identical()
to test for identity:
identical(a, b)
## [1] FALSE
Corresponding elements of a
and b
have the same values, but the two vectors don’t have the same set of names, so they aren’t considered identical.
Here’s another way to see that “evaluating to the same value” is not the same as “identity”:
TRUE == 1
## [1] TRUE
When TRUE (itself of
type logical
) is being compared with something numerical (type integer
or double
) it is coerced into the numerical vector 1. (In the same situation FALSE
would be coerced to 0.) But clearly TRUE
and 1 are not identical:
identical(TRUE, 1)
## [1] FALSE
2.4.1.3 And, Or, Not
We consider an “and” statement to be true when both of its component statements are true; otherwise it is counted as false. The &
Boolean operator accords with our thinking:
<- c(TRUE, TRUE, FALSE, FALSE)
a <- c(TRUE, FALSE, TRUE, FALSE)
b & b a
## [1] TRUE FALSE FALSE FALSE
In logic and mathematics, an “or” statement is considered to be true when at least one of its component statements are true. (This is sometimes called the “inclusive” use of the term “or.”) R accords with this line of thinking:
<- c(TRUE, TRUE, FALSE, FALSE)
a <- c(TRUE, FALSE, TRUE, FALSE)
b | b a
## [1] TRUE TRUE TRUE FALSE
The &&
and ||
operators follow the “and” and “or” logic respectively, but are applied only to the first elements of the vectors being compared:
<- c(TRUE, TRUE)
a <- c(TRUE, TRUE)
b && b a
## [1] TRUE
<- c(TRUE, FALSE)
a <- c(FALSE, FALSE)
b || b a
## [1] TRUE
These operators will come in handy later on, when we study conditionals.
The final Boolean operator is !
, which works like “not”:
<- c(TRUE, FALSE)
a !a
## [1] FALSE TRUE
<- c(2, 5, 6)
e <- c(3, 1, 2)
f > f e
## [1] FALSE TRUE TRUE
!(e > f)
## [1] TRUE FALSE FALSE