In R Language Definition, NA
values are briefly described, a portion of which says
... In particular,
FALSE & NA
isFALSE
,TRUE | NA
isTRUE
.NA
is not equal to any other value or to itself; testing forNA
is done usingis.na
. However, anNA
value will match anotherNA
value inmatch
.
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
identical(NA,NA_integer_)
returns false, so that part isn't really that surprising.match
, that just seems like one of millions of at least partially subjective design decisions that happen when writing a language. The only real answer is probably that it made sense to whoever wrote it originally.?identical
has an argumentsingle.NA
which if set toFALSE
will return FALSE. It tests by default if they're conceptually equal, as opposed to==
. From?==
, "NAs are non-comparables even to themselves". I'm guessingmatch
also has a conceptual touch to it, as it mentions: "Exactly what matches what is to some extent a matter of definition." No idea about?all.equal
though. It doesn't even mentionNA
AFAICT.