identical(NA, NA)
returns TRUE
, but the following code filters NA
out of the date frame:
library(tidyverse)
filter(starwars, birth_year == birth_year)
If NA
does equal NA
the starwars filtered data frame above should include birth years of NA
. Why doesn't it?
NA is
identical
to NA, but doesn't equal it. If you runNA==NA
, the response will be NA, because the equal operator doesn't apply to NAs. From theidentical
documentation:And from the documentation for
==
:The rationale is that missing values, at a conceptual level, are not the same as one another. They could potentially represent very different values, but we just don't know what those values are.
An alternative in this situation is to add
| is.na(birth_year)
.