In R Language Definition, NA
values are briefly described, a portion of which says
... In particular, FALSE & NA
is FALSE
, TRUE | NA
is TRUE
. NA
is not equal to
any other value or to itself; testing for NA
is done using is.na
. However, an NA
value will match another NA
value in match
.
Regarding the statement "NA
is not equal to
any other value or to itself",
Updated:
The question, revised again, is
What is the reasoning, if any, behind NA
matching NA
in match
, and nowhere else in the language?
It doesn't make sense to me that a missing value, unknown by anyone (or it would not be missing), would match another missing value of the same type. Since I posted this, I came across something in example(match)
that provides some reasoning. Character coercion changes its type. I can erase it completely if I like.
match(NA, NA)
# [1] 1
match(NA, NA_real_)
# [1] 1
match(NA_character_, NA_real_)
# [1] 1
match(paste(NA), NA)
# [1] NA
gsub("NA", "", NA)
# [1] NA
gsub("NA", "", paste(NA))
# [1] ""
is.na(NA)
# [1] TRUE
is.na(paste(NA))
# [1] FALSE
Apologies for stirring the pot, but some of the documentation is unclear about this. It might boil down to the R parser/deparser and the fact that you can turn anything into a text character object in R.
Original Post:
Now referring to "However, an NA
value will match another NA
value in match
."
If NA
is it not equal to itself, why is it matched with itself in match
? and also in identical
? Is this done on purpose?
NA == NA ## expecting TRUE
# [1] NA
NA != NA
# [1] NA
x <- NA
x == x
# [1] NA
match(NA, NA)
# [1] 1
identical(NA, NA)
# [1] TRUE
all.equal(NA, NA)
# [1] TRUE
See Question&Answers more detail:
os