R Tutorial R Charts & Graphs R Statistics R References

R - Logical OR operator



In R, Logical OR operator || combines first elements of two vectors 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, -1, FALSE, 5+2i)

#Applying || operator
print(v1 || v2)

The output of the above code will be:

[1] TRUE

Example: using with matrices

Similarly, the || operators can be used with matrices.

m1 <- matrix(c(10, 0, TRUE, 1+2i), nrow=2)
m2 <- matrix(c(20, -1, FALSE, 5+2i), nrow=2)

#Applying || operator
print(m1 || m2)

The output of the above code will be:

[1] 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