[Rd] subset and missing value indexing

From: Adaikalavan Ramasamy <ramasamy_at_cancer.org.uk>
Date: Wed 18 Aug 2004 - 03:55:58 EST


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



R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed Aug 18 03:59:11 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 08:59:19 EST