R - Element-wise Logical OR operator
In R, element-wise Logical OR operator | combines each element of the first vector with the corresponding element of the second vector and returns TRUE if any of the elements is TRUE, else returns FALSE. It can be applied on vectors of type logical, numeric or complex. All numbers other than 0 are considered as logical value TRUE.
This operator can also be used to combine conditions and returns True when any of the conditions is true.
Example: using with vectors
Consider the example below, where | operators is used with vectors.
v1 <- c(10, 0, TRUE, 1+2i) v2 <- c(20, 0, FALSE, 5+2i) #Applying | operator print(v1 | v2)
The output of the above code will be:
[1] TRUE FALSE TRUE TRUE
Example: using with matrices
Similarly, the | operators can be used with matrices. All operations are performed element-wise.
m1 <- matrix(c(10, 0, TRUE, 1+2i), nrow=2) m2 <- matrix(c(20, 0, FALSE, 5+2i), nrow=2) #Applying | operator print(m1 | m2)
The output of the above code will be:
[,1] [,2] [1,] TRUE TRUE [2,] FALSE TRUE
Example: Combining conditions
Consider the example below, where | operators is used to combine conditions.
i <- 50 if (i < 100 | i > 200) { sprintf("%d do not lie in range [100, 200].", i) } else { sprintf("%d lies in range [100, 200].", i) }
The output of the above code will be:
[1] "50 do not lie in range [100, 200]."
❮ R - Operators