The Complete Guide to Logical Operators in R (2024)

Logical operators allow us to change or compare the results of the comparisons made using relational operators. Learn everything you need to know about them!

The Complete Guide to Logical Operators in R (1)

Suppose we want to change or compare the results of the comparisons made using relational operators. How would we go about doing that?

R does this using the AND, the OR, and the NOT operator.

Logical Operators

  • AND operator &
  • OR operator |
  • NOT operator !

The AND operator takes two logical values and returns TRUE only if both values are TRUE themselves. This means that TRUE & TRUE evaluates to TRUE, but that FALSE & TRUE, TRUE & FALSE, and FALSE & FALSE evaluates to FALSE.

The Complete Guide to Logical Operators in R (2)

Instead of using logical values, we can use the results of comparisons. Suppose we have a variable x, equal to 12. To check if this variable is greater than 5 but less than 15, we can use x greater than 5 and x less than 15.

x <- 12
x > 5 & x < 15

The first part, x > 5 will evaluate to TRUE since 12 is greater than 5. The second part, x < 15 will also evaluate to TRUE since 12 is also less than 15. So, the result of this expression is TRUE since TRUE & TRUE is TRUE. This makes sense, because 12 lies between 5 and 15.

However, if x were 17, the expression x > 5 & x < 15 would simplify to TRUE & FALSE, which results in the expression being FALSE.

For you to try

Consider the following vector and variable:

linkedin <- c(16, 9, 13, 5, 2, 17, 14)
last <- tail(linkedin, 1)

The linkedin vector represents the number of LinekdIn views you profile has gotten in the last seven days. The last variable represents the last value of the linkedin vector.

Determine whether the last variable is between 15 and 20, excluding 15 but including 20.

Solution

# We are looking for the R equivalent of 15 < last <= 20
last > 15 & last <= 20
The Complete Guide to Logical Operators in R (3)

For you to try (2)

Consider the following vectors:

linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)

The linkedin vector represents the views on your LinkedIn profile from the past 7 days, and the facebook vector represents the views on your Facebook profile from the past 7 days.

Determine when LinkedIn views exceeded 10 and Facebook views failed to reach 10 for a particular day. Use the linkedin and facebook vectors.

Solution

# linkedin exceeds 10 but facebook below 10
linkedin > 10 & facebook < 10
The Complete Guide to Logical Operators in R (4)

For you to try (3)

Consider the following matrix:

views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)

The linkedin and facebook variable corresponds to the same vectors in the previous for you to try.

The matrix views has the first and second row corresponding to the linkedin and facebook vectors, respectively.

Determine when the views matrix equals to a number between 11 and 14, excluding 11 and including 14.

Solution

# When is views between 11 (exclusive) and 14 (inclusive)?
views > 11 & views <= 14
The Complete Guide to Logical Operators in R (5)

The OR operator (|) works similarly, but the difference is that only at least one of the logical values should be equal to TRUE for the entire OR operation to evaluate to TRUE.

This means that TRUE | TRUE equals TRUE, but also, TRUE | FALSE and FALSE | TRUE evaluates to TRUE. When both logicals are FALSE in an OR operation, so in the case of FALSE | FALSE, the result is FALSE. Remember, the OR operation is not an exclusive or operation, so TRUE | TRUE equals TRUE as well.

The Complete Guide to Logical Operators in R (6)

With the AND operator, only TRUE & TRUE makes a TRUE, anything else is FALSE. With the OR operator, only FALSE | FALSE makes a FALSE, anything else is TRUE.

Just as for AND operations, we can use comparisons together with the OR operator. Suppose we have a variable y, equal to 4. To see if this variable is less than 5 or greater than 15, we can use the following expression:

y <- 4
y < 5 | y > 15

R will first carry out the comparisons, resulting in TRUE | FALSE, which in turn results in TRUE.

Now, suppose y is 14. The expression y < 5 | y > 15 now evaluates to FALSE | FALSE. Neither one of the comparisons are TRUE, so the result is FALSE.

For you to try

Using the same variables from the last for you to try, determine if last is under 5 or above 10.

linkedin <- c(16, 9, 13, 5, 2, 17, 14)
last <- tail(linkedin, 1)

Solution

# Is last under 5 or above 10?
last < 5 | last > 10
The Complete Guide to Logical Operators in R (7)

For you to try (2)

Consider the same linkedin and facebook vectors from earlier exercises.

linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)

Determine when one or both social profiles were visited at least 12 times.

Solution

# When were one or both visited at least 12 times?
linkedin >= 12 | facebook >= 12
The Complete Guide to Logical Operators in R (8)

The NOT operator, represented by an exclamation mark !, simply negates the logical value it is used on. That is, !TRUE evaluates to FALSE, while !FALSE evaluates to TRUE.

The Complete Guide to Logical Operators in R (9)

Just like the OR and AND operators, we can use the NOT operator in combination with logical operators. This is not always necessary. For example, !(x < 5) is the same as x >= 5.

However, there are cases in R where the NOT operator is especially handy. For example, the built-in R function, is.numeric() checks if an R object is a numeric. There is no respective built-in function that checks if it isn’t a numeric. To check, we would have to negate the result ( !is.numeric()). So, is.numeric(5) evaluates to TRUE, as 5 is a numeric. If we negate this result using the NOT operator (!is.numeric(5)), we get FALSE. If, however, we use is.numeric("hello") we get FALSE. Negating this result ( !is.numeric("hello")) gives us TRUE.

Now, how do logical operators work with vectors and matrices? Just as relational operators, they perform the operations element-wise. Consider theses two vectors:

c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)

The AND operation on these two vectors, results in a vector with the elements TRUE, FALSE, and FALSE.

TRUE FALSE FALSE

The first elements in both vectors are TRUE, so the first element of the resulting vector contains TRUE. Similarly, for the second elements where TRUE & FALSE result in FALSE, and in the third elements, where FALSE & FALSE give FALSE.

A similar thing happens with the OR operator:

c(TRUE, TRUE, FALSE) | c(TRUE, FALSE, FALSE)

TRUE | TRUE gives TRUE, TRUE | FALSE also gives TRUE, and FALSE | FALSE gives FALSE. So, we would get the result:

TRUE TRUE FALSE

The NOT operator also works on every element on the vector:

!c(TRUE, TRUE, FALSE)

TRUE are converted to FALSE, and FALSE are converted to TRUE. So, we would get the result

FALSE FALSE TRUE
The Complete Guide to Logical Operators in R (10)

For you to try

What would the following set of R expressions return:

x <- 5
y <- 7
!(!(x < 4) & !!!(y > 12))

Solution

FALSE

To determine the answer, it is helpful to break down the query to smaller expressions:

We first have the left expression !(x < 4) of the inner expression (!(x < 4) & !!!(y > 12)).

  1. x < 4 — since x is 5, and 5 < 4 is not true, this statement evaluates to FALSE
  2. !(x < 4) — From the step above, we determined that x < 4 evaluates to FALSE. Negating this result gives us !FALSE, which is TRUE.

Next, we have the right expression !!!(y > 12) of the inner expression (!(x < 4) & !!!(y > 12)).

  1. y > 12 — Since y is 7, and 7 > 12 is not true, this expression evaluates to FALSE .
  2. !(y > 12) — Negating the result from step 1, we get !FALSE, or TRUE.
  3. !!(y > 12) — Negating the result from step 2, we get !TRUE, or FALSE.
  4. !!!(y > 12) — Negating the result from step 3, we get !FALSE, or TRUE.

So for the inner expression (!(x < 4) & !!!(y > 12)), it evaluates to TRUE & TRUE, which equals TRUE.

The outer NOT operator ! negates this TRUE making !(!(x < 4) & !!!(y > 12)) equal to !TRUE or FALSE.

What is the difference between a single and a double ampersand or vertical bar? In R, you can use both the single sign version or the double sign version, but the result of the logical operation you’re carrying out can be different. The biggest difference occurs when you use the two types of operations on vectors.

c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)

As we’ve seen before, the above expression evaluates to a vector:

TRUE FALSE FALSE

However, if we use double ampersand, we simply get TRUE.

c(TRUE, TRUE, FALSE) && c(TRUE, FALSE, FALSE)

This is because the double ampersand operation only examines the first element of each vector. In this case, the first elements are TRUE and TRUE, so the expression returns TRUE.

The Complete Guide to Logical Operators in R (11)

You can see similar things happening with the OR operator. The single sign version | returns and entire vector. The double sign version || returns the result of the OR operator on the first element of each vector.

The Complete Guide to Logical Operators in R (12)

So pay attention when doing logical operations on vectors. You will likely want to use the single sign version.

All images, unless specified, are owned by the author. The banner image was created using Canva.

The Complete Guide to Logical Operators in R (2024)
Top Articles
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 6239

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.