Logical vectors in R can contain NA in addition to TRUE and FALSE. An NA in a logical context can be interpreted as “unknown.” Consider the following commands:
4 > 3 & NA
## [1] NA
4 < 3 & NA
## [1] FALSE
4 < 3 | NA
## [1] NA
4 > 3 | NA
## [1] TRUE
Explain why there is a difference in the output of these four commands. Why are they not all NA?
& operator needs to evaluate all the conditions starting from left and return TRUE only when all the conditions are TRUE. It will return FALSE as soon as the first FALSE conditions is encountered. It may also return NA if NA is encountered.
Hence first command returns NA and second command returns FALSE.
| (or) operator will need to evaluate all the conditions and return TRUE if any of the conditions is TRUE (best case). Thats why fourth one returns TRUE.
Third one (4 < 3 | NA) returns NA because the best case is NA. 4<3 is FALSE but | operator needs to evaluate if the next condition is true or not FALSE and it finds that it is NA which is returned.
Get Answers For Free
Most questions answered within 1 hours.