[R] NA and logical indexes

Sebastian P. Luque spluque at gmail.com
Fri Nov 28 22:25:36 CET 2008


Hi,

I vaguely remember this issue being discussed at some length in the
past, but am having trouble relocating the proper thread (defining an
adequate search string to do so):

---<---------------cut here---------------start-------------->---
R> foo <- data.frame(A=gl(2, 5, labels=letters[1:2]), X=runif(10))
R> foo$A[1] <- NA
R> foo$A == "b"
 [1]    NA FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
R> foo$A[foo$A == "b"]
[1] <NA> b    b    b    b    b   
Levels: a b
R> foo$X[foo$A == "b"]
[1]     NA 0.4425 0.7164 0.3171 0.1967 0.8300
R> foo[foo$A == "b", ]
      A      X
NA <NA>     NA
6     b 0.4425
7     b 0.7164
8     b 0.3171
9     b 0.1967
10    b 0.8300
---<---------------cut here---------------end---------------->---

Why is foo$X[1] set to NA in that last call?


Cheers,

-- 
Seb



More information about the R-help mailing list