Home » R Programming » Logical Operators in R

Logical Operators in R

In this tutorial you will learn about the logical operators in R with examples. Like most of the programming languages, R programming also has logical operators to perform Boolean operations.

Logical Operators in R

Following symbols are used as logical operators in R programming language:

Operator Symbols Logical Operation Example
! Logical negation NOT ! x
& Element-wise logical AND x & y
&& Vector logical AND x && y
| Element-wise logical OR x | y
|| Vector logical OR x || y
xor Element-wise exclusive OR xor(x,y)

Suppose we have two relational expressions A and B.

Following table shows the result of various logical operations on A and B:

A B !A A && B A || B xor(A,B)
TRUE TRUE FALSE TRUE TRUE FALSE
FALSE TRUE TRUE FALSE TRUE TRUE
TRUE FALSE FALSE FALSE TRUE TRUE
FALSE FALSE TRUE FALSE FALSE FALSE

Logical Expressions

A logical expression is an expression created using the relational operators and the logical operators. The value of the logical expression is either TRUE or FALSE. The example of logical expression is :

x <- 5
x < 4 & x >= 2 
[1] FALSE

In the above R code, x < 4 and x >= 2 are the relational expressions. R evaluate the two relational expressions and in next statement, it evaluates the logical expression x < 4 & x >= 2.

Examples of Logical Operators in R

To understand the logical operators in R, lets understand logical operator in R with examples.

Logical NOT (!)

The symbol (!) is the negation operator used for logical NOT.
For example,

if A is TRUE then !A is FALSE
If A is FALSE then !A is TRUE

A <- TRUE
! A
[1] FALSE
B <- FALSE
! B
[1] TRUE

Logical AND (&)

The symbol & (single ampersand) is used for element-wise logical AND operator. If both the operands are TRUE, result using logical and (&) evaluate as TRUE else result using logical and (&) evaluate as FALSE.

A <- TRUE
B <- FALSE
C <- TRUE
A & B
[1] FALSE

In the above R code, A is TRUE and B is FALSE. Hence the result of the A & B is FALSE.

A & C
[1] TRUE

In the above R code A is TRUE and C is also TRUE, the result of the A & C using logical and & evaluate as TRUE.

Logical OR (|)

The symbol | is used for element-wise logical OR. The logical expression A | B is TRUE if A or B or both are TRUE (i.e., at least one of the logical value is TRUE).

# A is TRUE, B is TRUE
A | B
[1] TRUE
# A is TRUE, C is FALSE
A | C
[1] TRUE

Logical Exclusive OR (xor())

The function xor() is used for element-wise exclusive logical OR. The result of xor(A,B) is TRUE if either A or B are TRUE but not both.

xor(TRUE,TRUE)
[1] FALSE
xor(TRUE,FALSE)
[1] TRUE
xor(FALSE,TRUE)
[1] TRUE
xor(FALSE,FALSE)
[1] FALSE

Remember the difference between OR (|) and XOR (xor()). In the case of using OR (A | B) operator, if at least one operand is TRUE, the result is TRUE otherwise the result is FALSE. In the case of using XOR (xor(A, B)) operator, the result is TRUE if either A or B is TRUE but not both.

Logical operations on Vector and scalar

Like arithmetic operators and relational operators, logical operations are also vectorized. When vector is compared with scalar using logical operator, R will recycled the scalar element to the length of vector and then perform element-wise logical operations.

Logical NOT (!)

x <- c(12, 10, 8, 16, 6)
! (x < 10)
[1]  TRUE  TRUE FALSE  TRUE FALSE

In the above R code, x is a vector and 10 is a scalar.

In this example, R evaluate the relational expression x < 10 resulting to a logical vector FALSE, FALSE, TRUE, FALSE, TRUE. Later it evaluates using logical NOT (!) operation on the logical vector,results as TRUE, TRUE, FALSE, TRUE, FALSE.

Logical AND (&)

x <= 8 & x >= 5
[1] FALSE FALSE  TRUE FALSE  TRUE

In the above examle, R evaluate the relational expression x <= 8 result to FALSE, FALSE, TRUE, FALSE, TRUE.The relational expression x >= 5 result to TRUE, TRUE, TRUE, TRUE, TRUE.

Later, it apply the logical AND (&) on both the logical vectors, final result as FALSE, FALSE, TRUE, FALSE, TRUE.

Vector Logical AND (&&)

x <= 8 && x >= 5
[1] FALSE

In the above example, R evaluate the relational expression x <= 8 result to FALSE, FALSE, TRUE, FALSE, TRUE. The relational expression x >= 5 result to TRUE, TRUE, TRUE, TRUE, TRUE.

Later, it apply the vector logical AND (&&) on both the logical vectors,final result as FALSE.

Note that the operator && evaluates from left to right and examine only the first element of both the vectors.

The shorter form & performs element-wise comparison, whereas the longer form && evaluates left to right examining only the first element in each vector.

Logical OR (|)

x >= 8 | x > 6
[1]  TRUE  TRUE  TRUE  TRUE FALSE

In the above R code, R evaluate the relational expression x >= 8 result to TRUE, TRUE, TRUE, TRUE, FALSE.The relational expression x >6 result to TRUE, TRUE, TRUE, TRUE, FALSE.

Later, it apply the logical OR (|) on both the logical vectors, final result as TRUE, TRUE, TRUE, TRUE, FALSE.

x >= 8 || x <= 6
[1] TRUE

In the above example, R evaluate the relational expression x >= 8 result to TRUE, TRUE, TRUE, TRUE, FALSE. The relational expression x <= 6 result to FALSE, FALSE, FALSE, FALSE, TRUE.

Later,it apply the vector logical OR (||) on both the logical vectors, result as TRUE.

Note that the operator || evaluates from left to right and examine only the first element of both the vectors.

The shorter form | performs element-wise comparison, whereas the longer form || evaluates left to right examining only the first element in each vector.

Logical Exclusive OR (xor)

The logical exclusive OR operator xor(a,b) is used when you want either a or b TRUE but not both.

x <- c(12, 10, 8, 16, 6)
xor(x >= 10, x >= 6)
[1] FALSE FALSE  TRUE FALSE  TRUE

In the aboe R code, R evaluate the relational expression x >= 10 result as TRUE, TRUE, FALSE, TRUE, FALSE. The relational expression x >= 6 result to TRUE, TRUE, TRUE, TRUE, TRUE.

Later,it apply the exclusive logical OR (xor) on both the logical vectors,final result as FALSE, FALSE, TRUE, FALSE, TRUE.

Logical operations on Vectors

x <- c(12, 10, 8, 16, 6)
y <- c(10, 12, 8, 18, 6)

Logical NOT (!)

! (x <= y) 
[1]  TRUE FALSE FALSE FALSE FALSE

R evaluate the relational expression x <=y result to a logical vector FALSE, TRUE, TRUE, TRUE, TRUE.

Later, it apply the negation (!) to the logical vector, final result as TRUE, FALSE, FALSE, FALSE, FALSE.

Logical AND (&)

x <= y & x >= 10
[1] FALSE  TRUE FALSE  TRUE FALSE

R evaluate the relational expression x <= y result to FALSE, TRUE, TRUE, TRUE, TRUE. The relational expression x >= 10 result to TRUE, TRUE, FALSE, TRUE, FALSE.

Later,it apply the logical AND (&) on both the logical vectors,final result as FALSE, TRUE, FALSE, TRUE, FALSE.

Logical AND (&&)

x <= y && x >= 10
[1] FALSE

In the above example, R evaluate the relational expression x <= y result to FALSE, TRUE, TRUE, TRUE, TRUE . The relational expression x >= 10 resulting to TRUE, TRUE, FALSE, TRUE, FALSE.

Later, it apply the vector logical AND (&&) on both the logical vectors,final result as FALSE.

Logical OR (|)

x >= y | x <= 12
[1]  TRUE  TRUE  TRUE FALSE  TRUE

Logical OR (||)

x >= y || x <= 12
[1] TRUE

Logical OR (xor)s

xor(x>=y,x<=12)
[1] FALSE  TRUE FALSE FALSE FALSE

Note that whenever we use logical operators on vectors of unequal length, R display a warning message and recycled the shorter vector to match the length of longer vector.

Logical operations on Vectors of unequal length

When we use logical operations on vectors of unequal length, R display a warning message and recycle the elements of shorter vector to match the length of longer vector and then perform the logical operations.

Logical NOT (!)

x <- c(12, 10, 8, 16, 6)
y <- c(10, 12, 8)
! (x <= y)
Warning in x <= y: longer object length is not a multiple of shorter object
length
[1]  TRUE FALSE FALSE  TRUE FALSE

As vector y is shorter than x, R display the warning message but apply the recycling rule to make the length of y similar to vector x and execute the logical operations.

Logical AND (&)

x <- c(12, 10, 8, 16, 6)
y <- c(10, 12, 8)
x <= y & x == y
Warning in x <= y: longer object length is not a multiple of shorter object
length
Warning in x == y: longer object length is not a multiple of shorter object
length
[1] FALSE FALSE  TRUE FALSE FALSE

First the elements of y are recycled to match the length of x. After applying the recycling rule, R evaluate the relational expressions x <= y and x == y and evaluate the logical expression.

Logical AND (&&)

x <- c(12, 10, 8, 16, 6)
y <- c(10, 12, 8)
x <= y && x == y
Warning in x <= y: longer object length is not a multiple of shorter object
length
[1] FALSE

Logical OR (|)

x <- c(12, 10, 8, 16, 6)
y <- c(10, 12, 8)
x <= y | x == y
Warning in x <= y: longer object length is not a multiple of shorter object
length
Warning in x == y: longer object length is not a multiple of shorter object
length
[1] FALSE  TRUE  TRUE FALSE  TRUE

Logical OR (||)

x <- c(12, 10, 8, 16, 6)
y <- c(10, 12, 8)
x <= y || x == y
Warning in x <= y: longer object length is not a multiple of shorter object
length
Warning in x == y: longer object length is not a multiple of shorter object
length
[1] FALSE

Logical exclusive OR (xor)

x <- c(12, 10, 8, 16, 6)
y <- c(10, 12, 8)
xor(x <= y , x == y)
Warning in x <= y: longer object length is not a multiple of shorter object
length
Warning in x == y: longer object length is not a multiple of shorter object
length
[1] FALSE  TRUE FALSE FALSE  TRUE

Logical operators for indexing

Logical operators are also used for selecting the elements of vector or data frame based on some logical conditions.

x <- c(12, 10, 8, 16, 6)
y <- c(10, 12, 8, 18, 6)

Suppose we need to extract only those elements of x which are less than or equal to 10 and greater than or equal to 8. To do this we can use the logical condition x <=10 & x >= 8 as an index for vector x.

x[x <= 10 & x >= 8]
[1] 10  8

Suppose we need to extract those elements of x which are not equal to y and x < y. To do this we can use the logical condition x != y & x < y as an index for vector x`.

x[x != y & x < y]
[1] 10 16

Endnote

In this tutorial you learned about logical operators in R and how R uses recycling rule while performing logical operations.

To learn more about other operators in R, please refer to the following tutorials:

Assignment operators in R
Arithmetic operators in R
Relational operators in R
Miscellaneous operators in R
Precendence of Operators in R
Operators in R

Hopefully you enjoyed learning this tutorial on logical operators in R. Hope the content is more than sufficient to understand logical operators in R.

Leave a Comment