1 == 1[1] TRUE
"a" == "a"[1] TRUE
"foo" == "bar"[1] FALSE
Almost all the commands you’ve seen so far have been some sort of mathematical operation (e.g., adding, dividing, computing the natural logarithm), but R can do much more for us. Logical comparisons are one particularly useful class of operations, in which we compare objects to test logical conditions.
We can test many flavors of logical conditions in R. Logical tests return a logical value as the result. A logical value is TRUE whenever the tested condition is satisfied and FALSE when the tested condition is not satisfied.
The “logical values” we describe here are actually length-one logical vectors. You’ll learn all about atomic vectors in the Vectors & Matrices module.
Do: Run the following code to create three objects that we can use below.
The simplest logical test is an equality check. To check if two objects are equal, we use the equality test operator, ==.
Notice that the equality test operator, ==, is two equals signs. Remember that a single equals sign is one of the possible assignment operators. You cannot use a single equals sign to do a logical comparison.
Mixing up == and = is one of the most common beginner mistakes (and experienced user typos) in R.
Predict: What will the following lines print?
y == x is FALSE because 5 is not equal to 7.y == w is TRUE because 5 is equal to 5.We can also check the usual greater-than/less-than conditions with >, <, >=, <=.
[1] FALSE
[1] FALSE
[1] TRUE
[1] TRUE
Given y <- 5 and w <- 5, predict what value each of the following expressions will return.
Use the interactive editor to check your predictions.
y and w store the same value, so:
y > w and y < w are both FALSE, since neither is strictly greater or less than the other.y >= w and y <= w are both TRUE, since “greater than or equal to” and “less than or equal to” are satisfied whenever the values are equal.We can negate any logical condition (i.e., change TRUE to FALSE and visa-versa) by prepending the ! character.
Predict: What will the following two lines print?
y == w is TRUE because 5 is equal to 5.!y == w is FALSE because we’ve negated the result of the equality test.Rather than negating an equality check, we will typically use the special “not-equal” operator, !=.
We can create more complex logical tests by combining multiple conditions with the AND and OR operators: & and |.
Logical AND tests if all conditions are satisfied.
Logical OR tests if at least one condition is satisfied.
To run multi-condition tests, we simply build up the desired logic by chaining together each constituent condition.
[1] TRUE
[1] FALSE
[1] TRUE
Predict: What will the following lines print?
FALSE because y is not both equal to itself and not equal to itself.FALSE because w is neither greater than itself nor less than itself.TRUE because x is not greater than itself, but x is less than or equal to itself.TRUE because x is both greater than or equal to itself and x is less than or equal to itself.Use a single line of code to generate a logical answer (i.e., TRUE/FALSE) to the following question.
Use numeric representations of the day, month, and year.
Let’s assume the current date is 25-08-2026. Then, today’s solution would be the following.
In R, most mathematical operators take precedence over logical tests. So, the parentheses in the solution are not strictly necessary.
That being said, the parentheses are still a good idea. In this case, the extra parentheses clarify exactly what we’re testing, and less ambiguity is almost always a good thing.
= and == in R?a <- 3 and b <- 4, what does a != b return?a is greater than 1 and less than 10.