Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (2024)

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, OR and NOT operators.

Logical Operators in R

  • AND Operator: Represented usingan ampersand, thisoperator takes two logical values and returns TRUE only if both values are TRUE themselves.
  • OR Operator:Denoted usingthe pike symbol, this operatortakes two logical values and returns TRUEif just one value is TRUE.
  • NOT Operator: Represented using an exclamation mark, this operatornegates the logical value it’s used on.

AND 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.

Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (1)

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

x <- 12x > 5 & x < 15

The first part, x > 5, will evaluate to TRUE because 12 is greater than five. The second part, x < 15, will also evaluate to TRUE because 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 five and 15.

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

More on R: Grouping Data With R: A Guide

AND Operator Example Problem

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 LinkedIn views your 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.

Example 1 Solution

# We are looking for the R equivalent of 15 < last <= 20last > 15 & last <= 20
Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (2)

AND Operator Example Problem 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 seven days, and the facebook vector represents the views on your Facebook profile from the past seven days.

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

Example 2 Solution

# linkedin exceeds 10 but facebook below 10linkedin > 10 & facebook < 10
Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (3)

AND Operator Example Problem 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 example.

The first and second rows in the matrix views corresponds to the linkedin and facebook vectors, respectively.

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

Example 3 Solution

# When is views between 11 (exclusive) and 14 (inclusive)?views > 11 & views <= 14
Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (4)

OR Operator “|”

The OR operator | works similarly to the AND operator &, but the difference is that only one of the logical values needs to 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, FALSE | FALSE, the result is FALSE. Remember, the OR operation is not an exclusive OR operation, so TRUE | TRUE equals TRUE as well.

Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (5)

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 in AND operations, we can use comparisons together with the OR operator. Suppose we have a variable y that equals four. To see if this variable is less than five or greater than 15, we can use the following expression:

y <- 4y < 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.

More on Data Science: 8 Ways to Filter Pandas DataFrames

OR Operator Example Problem 1

Using the same variables from the “AND operator example problem 3,” determine if last is under five or above 10.

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

Example 1 Solution

# Is last under 5 or above 10?last < 5 | last > 10
Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (6)

OR Operator Example Problem 2

Consider the same linkedin and facebook vectors from the previous example.

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.

Example 2 Solution

# When were one or both visited at least 12 times?linkedin >= 12 | facebook >= 12
Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (7)

NOT Operator “!”

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

Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (8)

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 five 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.

Logical Operators and Vectors

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. The same holds true 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
Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (9)

Logical Operators and Vectors Example Problem

What would the following set of R expressions return:

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

Solution

FALSE

To find the answer, it’s helpful to break the query down 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 five, 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 seven, and 7 > 12 is not true, this expression evaluates to FALSE.
  2. !(y > 12): Negating the result from step one, we get !FALSE, or TRUE.
  3. !!(y > 12): Negating the result from step two, we get !TRUE, or FALSE.
  4. !!!(y > 12): Negating the result from step three, 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.

More on Data Science: How to Show All Columns and Rows in a Pandas DataFrame

Single vs. Double Operators “&” vs “&&”, “|” vs “||”

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 &&, we simply get TRUE.

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

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

Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (10)

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

Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (11)

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

Guide to Logical Operators in R (OR, NOT and AND in R) | Built In (2024)

FAQs

When to use & vs && in R? ›

One thing to note is that && only works on single logical values, i.e., logical vectors of length 1 (like you would pass into an if condition), but & also works on vectors of length greater than 1.

What is the logical operator for and in R? ›

AND 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 .

What does %*% mean in R? ›

Matrix Multiplication

How do you say and OR in R? ›

R also has the logical operators && and || . According to the R help ( ?' &&' ), “& and && indicate logical AND and | and || indicate logical OR.

How does '&' differ from &&? ›

The key difference is that the & operator is a bitwise operator, while the && operator is a logical operator. Bitwise operators work on bits and perform "bit by bit" operations, they are applied to the bits of one or two operands.

When would it be most appropriate to use the && operator? ›

The && operator is most appropriate to use when you need to create an AND condition between two or more expressions.

What does %>% mean in R? ›

R pipes are a way to chain multiple operations together in a concise and expressive way. They are represented by the %>% operator, which takes the output of the expression on its left and passes it as the first argument to the function on its right. Using pipes in R allows us to link a sequence of analysis steps.

What are logical operators || and && used for give examples? ›

Logical Operators (&&, ||, !)
SymbolOperationDescription
&&ANDTrue only if both exp1 and exp2 are true; otherwise, false.
||ORTrue if either exp1 or exp2 is true; false (0) only if both are false.
!NOTFalse if exp1 is true; true if exp1 is false.

How is the and logical operator used? ›

Conditional logical AND operator &&

The result of x && y is true if both x and y evaluate to true . Otherwise, the result is false . If x evaluates to false , y isn't evaluated. The logical AND operator & also computes the logical AND of its operands, but always evaluates both operands.

What does \\ mean in R? ›

To match metacharacters in R you need to escape them with a double backslash “\\”. The following displays the general escape syntax for the most common metacharacters: The following provides examples to show how to use the escape syntax to find and replace metacharacters.

What does ## mean in R? ›

in R, a hashtag indicates a comment that will not be evaluated. You can use as many as you want: # is equivalent to ###### . It's a matter of style. I use two ## to indicate a comment so that it's clearer what is a comment versus what I don't want to run at the moment.

What does three dots mean in R? ›

This is known in R as the ellipsis argument, and it signals the possibility that one or more additional arguments may be supplied when the function is actually called.

What does $$ mean in R? ›

In R programming, the dollar sign serves as a versatile and powerful operator for extracting, manipulating, and creating variables within data frames, lists, and environments. Its concise syntax enhances the readability of code and contributes to the efficiency of data analysis workflows.

Is it better to use <- OR in R? ›

They have different order of operations precedence. If put into one line the '<-' assignment will be evaluated before the '=' assignment. Otherwise no difference.

When to use & and && in R? ›

Difference Between & and && in R
  1. The “&” operator performs the element-wise comparison and returns a logical vector of the same length as its input.
  2. the “&&” operator evaluates only the first element of the input and returns the single logical value.
Jun 9, 2023

What is the ampersand operator in R? ›

The AND operator in R is the ampersand. The OR operator also creates a new logical vector from two logical input vectors. Now the output element is TRUE if at least one of the input elements is TRUE . In R, the OR operator is a vertical bar.

What are logical operators || and && used for? ›

Logical Operators (&&, ||, !)
SymbolOperationDescription
&&ANDTrue only if both exp1 and exp2 are true; otherwise, false.
||ORTrue if either exp1 or exp2 is true; false (0) only if both are false.
!NOTFalse if exp1 is true; true if exp1 is false.

Can I use && and || in the same statement? ›

You can use the logical operators && (AND), || (OR) , and ! (NOT) within control expressions in an if statement. You must be careful to use extra parentheses in some situations since && (AND) executes before || (OR) according to the order of operations.

What differentiates the and operator from the AndAlso operator in terms of their evaluation behavior in conditional expressions? ›

In a Boolean comparison, the And operator always evaluates both expressions, which could include making procedure calls. The AndAlso Operator performs short-circuiting, which means that if expression1 is False , then expression2 is not evaluated.

Top Articles
Latest Posts
Article information

Author: Delena Feil

Last Updated:

Views: 6246

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.