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

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). Below table describes them

OPERATORS |
NAME |
DESCRIPTION |
EXAMPLE |
---|---|---|---|

& | logical AND | It will return true when both conditions are true | c(20, 30) & c(30, 10) |

&& | logical AND | Same as above but, It will work on single element | If (age > 18 && age <= 25) |

| | logical OR | It will returns true when at-least one of the condition is true | c(20, 30) | c(30, 10) |

|| | logical OR | Same as above but, It will work on single element | If (age == 35 || age < 60) |

! | logical NOT | If the condition is true, logical NOT operator returns as false | If age = 18 then !( age = 18) returns false. |

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

**LOGICAL AND Truth table**

Truth table behind the 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 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 will help you understand, how the logical operators in R Programming are used in If statements. For this example, We are assigned one integer variables and then, inside the If Statement we are using basic logical operators such as : &&, ||, and !.

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 12 |
# 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") } |

**OUTPUT**

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

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

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

From the below screenshot you can observe that, we have entered age = 72.

## R Logical Operators example

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

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# Logical Operators in R example num1 <- c(TRUE, FALSE, 0, 23) num2 <- c(FALSE, FALSE, TRUE, TRUE) # Performs logical AND operation on each and every element in both num1, num2 num1 & num2 # Performs logical AND operation on first element in both num1, num2 num1 && num2 # Performs logical OR operation on each and every element in both num1, num2 num1 | num2 # Performs logical 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 |

**OUTPUT**

ANALYSIS

First we declared two vectors

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

Below statement will compare each and every vector element, and find the logical relation.

1 |
num1 & num2 |

Following statement will compare the first element of num1 vector and the first element of num2 vector. It means, TRUE && FALSE = FALSE.

1 |
num1 && num2 |

Thank You for Visiting Our Blog.

## Leave a Reply