Logical operators (AND, OR) with NA, TRUE and FALSE

*爱你&永不变心* 提交于 2019-11-27 04:31:25

To quote from ?Logic:

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.

The key there is the word "ambiguous". NA represents something that is "unknown". So NA & TRUE could be either true or false, but we don't know. Whereas NA & FALSE will be false no matter what the missing value is.

Joshua Ulrich

It's explained in help("|"):

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.

From the examples in help("|"):

x <- c(NA, FALSE, TRUE)
names(x) <- as.character(x)
outer(x, x, "&") ## AND table
#        <NA> FALSE  TRUE
# <NA>     NA FALSE    NA
# FALSE FALSE FALSE FALSE
# TRUE     NA FALSE  TRUE

outer(x, x, "|") ## OR  table
#        <NA> FALSE TRUE
#  <NA>    NA    NA TRUE
# FALSE    NA FALSE TRUE
#  TRUE  TRUE  TRUE TRUE
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!