yoland
yoland

Reputation: 554

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

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.

Upvotes: 5

Views: 258

Answers (1)

TheDataGuy
TheDataGuy

Reputation: 371

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)

Upvotes: 1

Related Questions