This behavior is explained in the values section of the help file:
The value returned is TRUE if at least one of the values in x is TRUE, and FALSE if all of the values in x are FALSE (including if there are no values). Otherwise the value is NA.
As you note, this seems to differ from the behavior of more commonly used functions such as sum
and mean
, since the presence of NA values in vector arguments to these functions return NA. This problem in perception is cleared up by joran's answer which refers to the documentation from ?Logic
, to requote:
NA is a valid logical object. Where a component of x or y is NA, the result will be NA if the outcome is ambiguous. In other words NA & TRUE evaluates to NA, but NA & FALSE evaluates to FALSE. See the examples below.
So in the case of ambiguity, for example, the calculation of a mean where the vector contains NA, or NA | FALSE
where the missing value might be TRUE, NA will be the output. Whereas in other cases such as any(c(TRUE, NA))
or TRUE | NA
, the outcome is unambiguous despite the presence of a missing value. This logic may be clearer in @Floo0's answer and in some of the comments to the question.