Logical Comparisons

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.

Going Further

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.

Equality

The simplest logical test is an equality check. To check if two objects are equal, we use the equality test operator, ==.

1 == 1
[1] TRUE
"a" == "a"
[1] TRUE
"foo" == "bar"
[1] FALSE
Common Mistakes

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.

2 = 3
Error in `2 = 3`:
! invalid (do_set) left-hand side to assignment

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.

Greater-Than & Less-Than

We can also check the usual greater-than/less-than conditions with >, <, >=, <=.

# Is y greater than x?
y > x
[1] FALSE
# Is y greater than or equal to x?
y >= x
[1] FALSE
# Is y less than x?
y < x
[1] TRUE
# Is y less than or equal to x?
y <= x
[1] TRUE
Practice

Given y <- 5 and w <- 5, predict what value each of the following expressions will return.

y > w
y >= w
y < w
y <= w

Use the interactive editor to check your predictions.

y > w
[1] FALSE
y >= w
[1] TRUE
y < w
[1] FALSE
y <= w
[1] TRUE

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.

Logical Negation

We can negate any logical condition (i.e., change TRUE to FALSE and visa-versa) by prepending the ! character.

!TRUE
[1] FALSE
!FALSE
[1] TRUE
y > x
[1] FALSE
!y > x
[1] TRUE

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, !=.

y == w
[1] TRUE
y != w
[1] FALSE

Combining Conditions

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.

  • \(A\) AND \(B\) is true only when both Condition A and Condition B are satisfied.
  • \(A\) AND \(B\) is when Condition A, Condition B, or both are not satisfied.
TRUE & TRUE
[1] TRUE
TRUE & FALSE
[1] FALSE
FALSE & FALSE
[1] FALSE

Logical OR tests if at least one condition is satisfied.

  • \(A\) OR \(B\) is true when Condition A, Condition B, or both are satisfied.
  • \(A\) OR \(B\) is false only when both Condition A and Condition B are not satisfied.
TRUE | TRUE
[1] TRUE
TRUE | FALSE
[1] TRUE
FALSE | FALSE
[1] FALSE

To run multi-condition tests, we simply build up the desired logic by chaining together each constituent condition.

# Is y equal to w AND is y less than x?
y == w & y < x
[1] TRUE
# Is y equal to w AND is y greater than x?
y == w & y > x
[1] FALSE
# Is y equal to w OR is y greater than x?
y == w | y > x
[1] TRUE

Predict: What will the following lines print?

  1. FALSE because y is not both equal to itself and not equal to itself.
  2. FALSE because w is neither greater than itself nor less than itself.
  3. TRUE because x is not greater than itself, but x is less than or equal to itself.
  4. TRUE because x is both greater than or equal to itself and x is less than or equal to itself.
Practice

Use a single line of code to generate a logical answer (i.e., TRUE/FALSE) to the following question.

  • Is the sum of the current day and the current month strictly greater than the two-digit year?

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.

(25 + 8) > 26
[1] TRUE

In R, most mathematical operators take precedence over logical tests. So, the parentheses in the solution are not strictly necessary.

25 + 8 > 26
[1] TRUE

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.

Knowledge Check
  • What’s the difference between = and == in R?
  • If a <- 3 and b <- 4, what does a != b return?
  • Write an expression that checks whether a is greater than 1 and less than 10.
Back to top