The Logical operators in R programming are useful to combine two or more conditions, and perform the logical operations using & (Logical AND), | (Logical OR), and ! (Logical NOT).
The Comparison Operators are useful 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.
OPERATORS | NAME | DESCRIPTION | EXAMPLE |
---|---|---|---|
& | AND | It returns true when both conditions are true | c(20, 30) & c(30, 10) |
&& | AND | Same as the above but, It works on single element | If (age > 18 && age <= 25) |
| | OR | Same as logical OR but, It works on single-element | c(20, 30) | c(30, 10) |
|| | OR | If the condition is true, the logical NOT operator returns as false | If (age == 35 || age < 60) |
! | NOT | Same as the above but, It works on a single element | If 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 1 | Condition 2 | Condition 1 && Condition 2 |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
LOGICAL OR Truth table
The Truth table behind the Programming logical OR operator is as shown below:
Condition 1 | Condition 2 | Condition 1 || Condition 2 |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Basic Logical Operators in R example
This example helps you understand how the logical operators in this Programming are 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 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 the age is not greater than 18, so the First statement will print.
Let us see what happens when we change the values. From the screenshot below, see that we have entered age = 29. It means the 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 the age is between 36 and 60, so the third statement will print the output.
From the screenshot below, see that we have entered age = 72.
R Logical Operators example
This example helps you understand how each logical operator works. Remember, any positive integer value greater than zero is considered as Boolean TRUE, and 0 is 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
In these logical operators example, first, we declared two vectors
num1 <- c(TRUE, FALSE, 0, 23) num2 <- c(FALSE, FALSE, TRUE, TRUE)
The below statement compares each vector element and finds 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