In R, the logical OR operator is represented by | for element-wise comparison and || for a single comparison between two elements. It returns TRUE if one of the conditions is TRUE”.
If both conditions are FALSE, they will return FALSE. This means TRUE | TRUE returns TRUE and FALSE | FALSE returns FALSE, but TRUE | FALSE and FALSE | TRUE returns TRUE.
Syntax
x | y
Return Value
It returns TRUE if x or y is TRUE.
Visual Representation
Example 1: Basic usage of OR
x <- c(TRUE, FALSE, 0, FALSE)
y <- c(FALSE, TRUE, 1, 0)
x | y
Output
[1] TRUE TRUE TRUE FALSE
Example 2: Usage with different contexts
Let’s define a variable k, assign the value 19, and then check the value.
k <- 19
k < 21 | k > 10
Output
[1] TRUE
Differences Between | and ||
| performs element-wise comparison and is suitable for operations on vectors.
|| compares only the first elements of vectors and is primarily used in conditions where a single TRUE/FALSE result is sufficient.
Logical Operators
Operator | Description |
!x | Not x |
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 |
That’s all!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.