# R-alpha: x[NA]

**Thomas Lumley
**
thomas@biostat.washington.edu

*Mon, 22 Sep 1997 09:04:02 -0700 (PDT)*

On 22 Sep 1997, Peter Dalgaard BSA wrote:
>* Just curious. Current logic in both R and Splus has
*>*
*>* > (1:3)[c(T,F,NA)]
*>* [1] 1 NA
*>*
*<snip>
>* One reason could be that since NA is logical by default, you would be
*>* getting awkward consequences of the type x[NA] == real(0), but
*>* x[c(NA,1)] == c(NA,5.3), but why is NA logical by default, then?
*
The alternative would be worse. If one element of the index is not
logical then the whole index is converted, so
R> x<-1:4
R> x[c(F,T,F,T)]
[1] 2 4
R> x[c(F,T,F,NA)]
[1] 2 NA
but
R> x[c(F,T,F,4)]
[1] 1 4
That is, the presence of any NA would result in selecting only element
as.numeric(T)==1.
BTW, while you obviously can't mix logical and numerical references I
think it's unfortunate that you can't mix numeric and name-based
references like
R> names(x)<-letters[1:4]
R> x
a b c d
1 2 3 4
R> x[c("a",2)]
Error: subscript out of bounds
If you want a real example, suppose you had a model frame from which you
wanted to extract the response (in position 1) and the variables whose
names were in a vector nn
mf[,c(1,nn)]
appears to be the obvious solution.
Thomas Lumley
------------------------------------------------------+------
Biostatistics : "Never attribute to malice what :
Uni of Washington : can be adequately explained by :
Box 357232 : incompetence" - Hanlon's Razor :
Seattle WA 98195-7232 : :
------------------------------------------------------------
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-