Re: [R] subset without removing NAs

From: Marc Schwartz <marc_schwartz_at_me.com>
Date: Thu, 28 Apr 2011 15:29:41 -0500

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



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 28 Apr 2011 - 20:38:37 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 28 Apr 2011 - 20:40:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive