R OR Operator Tutorial with Example

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. 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. When both logicals are FALSE, the result is FALSE, unlike the exclusive-OR operation in which it returns TRUE.

One thing to remember 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. By using the OR operator, only FALSE | FALSE makes a FALSE; anything else is TRUE.


x | y

Return Value

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



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.

Example 2

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


[1] TRUE

Logical Operators in R

!xNot x
x | yelement-wise OR
x | | yLogical OR
x & yelement-wise AND
x && yLogical 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. Zero is considered FALSE, and non-zero numbers are taken as TRUE.

That is it for OR operator in R.

Leave a Comment