1

I have run into seemingly inconsistent results while using different combinations of logical tests in R. Here is a reproducible example:

x <- NA
y <- 1

is.na(x) # evaluates to TRUE
x == 2 # evaluates to NA

is.na(y) # evaluates to FALSE
y == 1 # evaluates to TRUE

is.na(x) & is.na(y) # evaluates to FALSE
is.na(x) & y == 1 # evaluates to TRUE

The above all evaluate as I would expect them to. The strange part (to me) is below:

x == 1 & y == 1 # evaluates to NA
x == 1 & is.na(y) # evaluates to FALSE
x == 1 & y == 0 # evaluates to FALSE

I am assuming x == 1 & y == 1 evaluates to NAbecause x is null, which prevents the logical evaluating to TRUE or FALSE. So, my question is why does changing the logical to x == 1 & is.na(y) or x == 1 & y == 0 actually result in FALSE? I would expect the latter two logical tests to result in NA as well because x == 1 can't be TRUE or FALSE.

rcj001
  • 13
  • 2
  • 3
    It is documented 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." – markus Jan 07 '20 at 23:06
  • Thanks for the link to the other post. That clears things up. – rcj001 Jan 07 '20 at 23:12

0 Answers0