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 NA
because 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
.