[R] NA values in indexing

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Mar 26 18:00:07 CET 2010


On Fri, Mar 26, 2010 at 4:15 PM, Matthew Dowle <mdowle at mdowle.plus.com> wrote:
> The type of 'NA' is logical. So x[NA] behaves more like x[TRUE] i.e. silent
> recycling.
>
>> class(NA)
> [1] "logical"
>> x=101:108
>> x[NA]
> [1] NA NA NA NA NA NA NA NA
>> x[c(TRUE,NA)]
> [1] 101  NA 103  NA 105  NA 107  NA
>
>> x[as.integer(NA)]
> [1] NA
>
> HTH

 Ouch. What is happening is that a numeric vector containing NA has
become a logical vector of all NA because "‘NA’ is a logical constant
of length 1" [?NA]

 Obviously R doesn't know that my c(NA,NA) came from c(4,NA,NA) and
not c(TRUE,NA,NA). And it prints them the same:

 > NA_integer_
 [1] NA
 > NA
 [1] NA


 Oh well, one to remember. I've got an urge to go eat a baNANA now.

Barry



More information about the R-help mailing list