From help("[")
:
When extracting, a numerical, logical or character NA index picks an
unknown element and so returns NA in the corresponding element of a
logical, integer, numeric, complex or character result, and NULL for a
list.
What does "corresponding element" mean? This can be understood if you know about recycling of vector elements. x[NA]
(this is a logical NA
per default) in your example is actually "interpreted" as x[c(NA, NA, NA, NA, NA)]
since logical indices are recycled. So, each element of x
has a corresponding NA
during subsetting and thus (per the quote above) NA
is returned for each element of x
. In layman's language: For each element of x
we don't know if we want it. Thus an unknown value is returned for each element.
As @r2evans points out: x[NA_integer_]
returns only one NA
because integer indices are not recycled. In layman's language: We want one value but don't know which one. Thus, one unknown value is returned.
NA
is perfectly meaningful and intentionally retained. It's the equivalent of SQL'snull
. There are times where I knowingly have anNA
in an index vector and subset based on it, knowing (relying on, even) that it will give me anNA
in that location. (In that case, though, it's typicallyNA_integer_
and not the elusivelogical
that defaults with[NA]
.) – Hitandrun