Logical operators are used to carrying out Boolean operations like AND, OR, and 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, and in that, we will talk about OR operators.
R OR Operator
The OR Operator in R is a logical operator that returns TRUE if one of the conditions is TRUE. If both conditions are FALSE, then it will return FALSE.
This means that TRUE | TRUE equals TRUE, but TRUE | FALSE and FALSE | TRUE returns to TRUE. Thus, when both logicals are FALSE, the result is FALSE, unlike the exclusive-OR operation in which it returns TRUE.
One thing to remember is that the OR operation is not an exclusive or operation, so TRUE | TRUE equals TRUE.
The difference between AND and OR operator is that the AND operator, only TRUE & TRUE, makes a TRUE; anything else is FALSE. Likewise, by using the OR operator, only FALSE | FALSE makes a FALSE; anything else is TRUE.
x | y
It returns TRUE if x or y is TRUE.
Let’s define two logical vectors, which means they 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 of them 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 and assign the value 19 and then check the value using 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 having a length of the longer operand. But && and || examines 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.
That is it for the OR operator in R.