R difference | and ||

The "|" symbol in R represents the logical OR operation. It is used to compare corresponding elements in two vectors and return a vector of the same length with TRUE for elements that are TRUE in either of the vectors.

The "||" symbol, on the other hand, is a logical OR operator that tests if at least one element of the first vector is TRUE. It only considers the first element of each vector when comparing.

For example:

# Using |
vector1 <- c(TRUE, FALSE, TRUE)
vector2 <- c(FALSE, TRUE, TRUE)

result <- vector1 | vector2
print(result)
# Output: TRUE FALSE TRUE

# Using ||
result <- vector1 || vector2
print(result)
# Output: TRUE

In the first case with "|", it compares corresponding elements and returns TRUE if at least one of the elements in the corresponding positions is TRUE. So, the output vector has TRUE for the positions where either vector1 or vector2 has TRUE values.

In the second case with "||", it checks only the first element of each vector. It returns TRUE if at least one of these first elements is TRUE. Therefore, even though in vector1 and vector2 there are TRUE values in other positions, since the first element in vector1 is TRUE, it returns TRUE without evaluating the rest of the elements.