[R] subset without removing NAs

Marc Schwartz marc_schwartz at me.com
Thu Apr 28 22:29:41 CEST 2011


On Apr 28, 2011, at 3:21 PM, Jannis wrote:

> On 04/28/2011 09:53 PM, Benjamin Caldwell wrote:
>> rws50<- subset(rw.fire.RW,shigo.av<50)
> 
> quick and dirty would be to replace all NAs with -99999 (or similar), use subset, and set all values ==-99999 in the subset back to NA. There may be more elegant solutions, though.
> 


Indeed. See ?is.na

DF <- data.frame(A = 1:20, B = c(1:15, rep(NA, 5)))

> subset(DF, B > 10)
    A  B
11 11 11
12 12 12
13 13 13
14 14 14
15 15 15

> subset(DF, (B > 10) | is.na(B))
    A  B
11 11 11
12 12 12
13 13 13
14 14 14
15 15 15
16 16 NA
17 17 NA
18 18 NA
19 19 NA
20 20 NA


HTH,

Marc Schwartz



More information about the R-help mailing list