Logical Operators in R

The Logical operators in R programming are used to combine two or more conditions, and perform the logical operations using & (Logical AND), | (Logical OR) and ! (Logical NOT).

The Comparison Operators are used to compare two variables, and what if we want to compare more than one condition? Very simple, R logical operators do the trick for you.

The table below describes the Logical Operators.

OPERATORSNAMEDESCRIPTIONEXAMPLE
&ANDIt returns true when both conditions are truec(20, 30) & c(30, 10)
&&ANDSame as the above but, It works on single elementIf (age > 18 && age <= 25)
|ORIt returns true when at-least one of the condition is truec(20, 30) | c(30, 10)
||ORSame as logical OR but, It works on single elementIf (age == 35 || age < 60)
!NOTIf the condition is true, logical NOT operator returns as falseIf age = 18 then !( age = 18) returns false.

Let us see the truth tables behind the logical operators in this programming for better understanding

LOGICAL AND Truth table

Truth table behind the R logical AND operator is as shown below:

Condition 1Condition 2Condition 1 && Condition 2
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

LOGICAL OR Truth table

The Truth table behind the Programming logical OR operator is as shown below:

Condition 1Condition 2Condition 1 || Condition 2
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Basic Logical Operators in R example

This example helps you understand how the logical operators in this Programming used in If statements.

For this logical operators example, we assigned one integer variable. Then, inside the If Statement, we are using basic logical operators such as &&, ||, and !. Please refer to the Comparison Operators in R article.

# Logical Operators in R example

age <- 16
if (!(age > 18)) {
  print("You are Too Young")
} else if(age > 18 && age <= 35) {
  print("Young Guy")
} else if(age == 36 || age <= 60) {
  print("You are Middle Age Person")
} else {
  print("You are too Old")
}

From the screenshot below, you can observe that we entered age = 16. It means age is not greater than 18, so the First statement printed.

Logical Operators in R Programming 1

Let us see what happens when we change the values. From the screenshot below, see that we have entered age = 29. It means age is between 18 and 35, so the Second statement is printed

From the screenshot below, observe that we have entered age = 45. It means age is between 36 and 60, so the third statement will prints the output.

From the screenshot below, see that we have entered age = 72.

Logical Operators in R Programming 4

R Logical Operators example

This example helps you understand how each logical operator work. Remember, any positive integer value greater than zero considered as Boolean TRUE, and 0 considered as Boolean False.

# Logical Operators example

num1 <- c(TRUE, FALSE, 0, 23)
num2 <- c(FALSE, FALSE, TRUE, TRUE)

# Performs AND operation on each element in both num1, num2
num1 & num2

# Performs AND operation on first element in both num1, num2
num1 && num2

# Performs OR operation on each element in both num1, num2
num1 | num2

# Performs OR operation on first element in both num1, num2
num1 || num2

This will convert all the num1 TRUE values to FALSE, and FALSE values to TRUE
!num1

# From num2 Vector - This will convert all the TRUE values to FALSE, and FALSE to TRUE
!num2
Logical Operators in R Programming 5

In these logical operators in r example, first, we declared two vectors

num1 <- c(TRUE, FALSE, 0, 23)
num2 <- c(FALSE, FALSE, TRUE, TRUE)

The below statement compare each vector element and find the logical relation.

num1 & num2

The following statement compares the first element of the num1 vector and the first element of the num2 vector. It means, TRUE && FALSE = FALSE.

num1 && num2