# Boolean operators && and ||

1 view

According to the R language definition, the difference between & and && (correspondingly | and ||) is that the former is vectorized while the latter is not.

According to the help text, I read the difference akin to the difference between an "And" and "AndAlso" (correspondingly "Or" and "OrElse")... Meaning: That not all evaluations if they don't have to be (i.e. A or B or C is always true if A is true, so stop evaluating if A is true)

Could someone shed light here? Also, is there an AndAlso and OrElse in R?

by (25.3k points)

The boolean operator & and | evaluate each element in a vector and return a vector.

For example:

x <- c (FALSE, TRUE,3,0)

y <- c (FALSE, TRUE, FALSE, TRUE)

x&y

x|y

Output:

 FALSE  TRUE FALSE FALSE

 FALSE  TRUE  TRUE  TRUE

While the boolean operators && and || evaluate only the first element of each vector and not evaluate the second element if the first element of each vector is enough to determine the value of the expression.

For example:

x <- c (FALSE, TRUE,3,0)

y <- c (FALSE, TRUE, FALSE, TRUE)

x&&y

x||y

Output

> x&&y

 FALSE

> x||y

 FALSE