Logical operators carry out Boolean operations like AND, OR, NOT, etc. The logical operators can operate on raw, logical, and number-like vectors. Moreover, logical operators allow us to change or compare the results. There are various types of operators available in R, and logical is one of them; we will talk about the OR operator.
OR in R
The OR in R is a built-in logical operator that returns TRUE if one of the conditions is TRUE. If both conditions are FALSE, they will return FALSE. This means that TRUE | TRUE equals TRUE, but TRUE | FALSE and FALSE | TRUE return to TRUE.
Thus, when both logicals are FALSE, the result is FALSE, unlike the exclusive-OR operation in which it returns TRUE. Remember that the OR operation is not an “exclusive or” operation, so TRUE | TRUE equals TRUE.
The difference between AND and OR operators is that the AND operator, only TRUE & TRUE, makes a TRUE; anything else is FALSE. Likewise, using the OR operator, only FALSE | FALSE makes a FALSE; anything else is TRUE.
When to use an OR operator in R?
The OR operator can be used when dealing with conditional statements (either True or False). They perform Logical OR operations.
x | y
It returns TRUE if x or y is TRUE.
How to Use OR in R?
Let’s define two logical vectors which will contain logical values.
x <- c(TRUE, FALSE, 0, FALSE) y <- c(FALSE, TRUE, 1, 0)
The OR operator( | ) performs an element-wise operation on the vectors. Let’s use OR operator and see the output.
x <- c(TRUE, FALSE, 0, FALSE) y <- c(FALSE, TRUE, 1, 0) x | y
 TRUE TRUE TRUE FALSE
The first three elements return TRUE because one is TRUE or 1. In the last element of both vectors, the values are FALSE, or 0, and both are FALSE. So, it returns FALSE.
Let’s define a variable k, assign the value 19 and then check the value using the OR operator.
k <- 19 k < 21 | k > 10
Logical Operators in R
|x | y||element-wise OR|
|x | | y||Logical OR|
|x & y||element-wise AND|
|x && y||Logical AND|
|isTRUE(x)||It tests if X is TRUE|
Operators & and | perform the element-wise operation, producing results with a more extended operand length. But && and || examine only the first element of the operands resulting in a single logical vector. Therefore, zero is considered FALSE, and non-zero numbers are taken as TRUE.
AND Operator in R
The AND operator in R is a built-in operator that takes two logical values and returns TRUE only if both values are TRUE.
TRUE & TRUE TRUE & FALSE FALSE & TRUE FALSE & FALSE
 TRUE  FALSE  FALSE  FALSE
You can see that it returns TRUE if both values are TRUE; otherwise, it returns FALSE.
You are not limited to using logical value with & operator. You can use the results of comparisons. For example, let’s say we have a variable equal to 11. To check if this variable is greater than 10 but less than 21, we can use “a” greater than 10 and a less than 21.
x <- 11 11 > 10 & 11 < 21
The OR in R returns TRUE if one of the conditions is TRUE. If both conditions are FALSE, then it will return FALSE. That is it for the OR operator in R.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.