[Rd] Bounds error when indexing a matrix with NA_character_
Milan Bouchet-Valat
nalimilan at club.fr
Sun Apr 10 13:08:05 CEST 2016
Hi!
I've just got a bounds error when trying to index a matrix with
NA_character_:
> y <- matrix(1:4, 2, 2, dimnames = list(A=c("a", "b"), B=c("a", "b")))
> y[NA,]
B
A a b
<NA> NA NA
<NA> NA NA
> y[NA_integer_,]
a
b
NA NA
> y[NA_character_,]
Error in y[NA_character_, ] : index out of
bounds
Yet, I would have sworn it behaves like integer NA (which apparently
isn't true since it also occurs with R 3.0). Assuming this isn't simply
a bug, what's the rationale behind this, and is it documented anywhere?
For example, ?Extract says:
> 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. (It returns ‘00’ for a raw
> result.)
An indeed that's how it works for vectors:
> x <- c(a=1)
> x[NA_character_]
<NA>
NA
The inconsistency between NA classes, and between vectors and matrices
is surprising.
Thanks for your comments.
More information about the R-devel
mailing list