Hi, I often have large dataframes with many variables and many NA's, from which I would like to subset out some rows. Here is a toy example:> x <- data.frame(a = c("x", "y", "z"), b = c(1, NA, 5)) > xa b 1 x 1 2 y NA 3 z 5 I realize that, if I know the values in x$b that I want to subset, things are easy:> x[x$b %in% c(1),]a b 1 x 1 However, if I only know the *range", then the NA's will flomux me.> x[x$b < 3,]a b 1 x 1 NA NA NA Of course, I can explicitly avoid the NA's by doing something like:> x[x$b < 3 & ! is.na(x$b),]a b 1 x 1 My problem is that this sort of syntax can become quite annoying when their are many variables in the subseting expression. That is, I want to writing something like: x[x$b < 3 & x$c > 5 & x$d > 100,] without having to write: x[! is.na(x$b) & ! is.na(x$c) & ! is.na(x$d) & x$b < 3 & x$c > 5 & x$d > 100,] Is there a trick for achieving this, for ignoring all NA's during subsetting? To the extent that it matters:> version_ platform sparc-sun-solaris2.6 arch sparc os solaris2.6 system sparc, solaris2.6 status Patched major 1 minor 4.0 year 2002 month 01 day 13 language R>Thanks, Dave Kane -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"David Kane <David Kane" <a296180 at mica.fmr.com> writes:> My problem is that this sort of syntax can become quite annoying when their are > many variables in the subseting expression. That is, I want to writing > something like: > > x[x$b < 3 & x$c > 5 & x$d > 100,] > > without having to write: > > x[! is.na(x$b) & ! is.na(x$c) & ! is.na(x$d) & x$b < 3 & x$c > 5 & x$d > 100,] > > Is there a trick for achieving this, for ignoring all NA's during subsetting?subset(x, b < 3 & c > 5 & d > 100) exists for that very reason... -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
----- Original Message ----- From: "David Kane <David Kane" <a296180 at mica.fmr.com> To: <r-help at stat.math.ethz.ch> Sent: Monday, April 08, 2002 10:38 PM Subject: [R] subsetting with NA's> without having to write: > > x[! is.na(x$b) & ! is.na(x$c) & ! is.na(x$d) & x$b < 3 & x$c > 5 & x$d >100,]> > Is there a trick for achieving this, for ignoring all NA's duringsubsetting? Have you tried na.omit()? Kevin -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Apparently Analagous Threads
- package argument to library as string
- rbind'ing empty rows in dataframes in 1.4.1 versus 1.5.0
- Strange behavior with saved character vectors containing a slash
- (PR#1577) is.na<- coerces character vectors to be factors
- Problems merging with POSIXct objects and all = TRUE