[Rd] subset and missing value indexing

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Tue Aug 17 19:55:58 CEST 2004


Out of curiosity, is this a bug or a feature or "==" ?

m <- matrix( 1:12, nc=4 )
f <- c("A", NA, "B", "A")

f == "A"
[1]  TRUE    NA FALSE  TRUE

m[ , f == "A"]         # equivalent to m[ , c(1, NA, 4) ]
     [,1] [,2] [,3]
[1,]    1   NA   10
[2,]    2   NA   11
[3,]    3   NA   12

m[ , which(f == "A")]
     [,1] [,2]
[1,]    1   10
[2,]    2   11
[3,]    3   12


In arguments section of help("which") it says that 
   'NA's are allowed and omitted (treated as if 'FALSE').

After some thinking, I think this might be due to subsetting using index
that includes missing value. help("[") appears not to say what happens
when one of the indexing element is a missing value, only that the index
can be logical ( and NA is logical ).

Is there any reason for allowing NA when subsetting ?

Regards, Adai



More information about the R-devel mailing list