Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%in% operator vs ==, handling of NA's

Tags:

r

I was wondering whats wrong with my code and found that the reason for my odd results was this unexpected handling of NA's by == and %in%.

> NA %in% NA
[1] TRUE
> NA == NA
[1] NA

Is there a reason for this? I've been reading about the == operator and its handling of NA's but couldn't find any information on why the %in% operator handles NA's differently.

like image 239
yoland Avatar asked Nov 08 '22 00:11

yoland


1 Answers

If you look at the NA documentation using ?"==", it states that "Missing values (NA) and NaN values are regarded as non-comparable even to themselves, so comparisons involving them will always result in NA. "

Note also that you can use the identical function i.e. identical(NA,NA)

like image 53
TheDataGuy Avatar answered Nov 15 '22 06:11

TheDataGuy