Hi,
The help page for subset states "subset: logical expression indicating elements or rows to keep: missing values are taken as false." Before I try to re-invent the wheel, I would like to know if one of the base or recommended packages would contain a variant of the subset function that would consider missing values as true. Thanks ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
Please read the "Details" section of the help file -- it essentially
tells you what to do by directly using indexing; e.g. for a vector, index by: [is.na(subset) | subset] . Adjust as necessary for your data structure.Or look at the code of, e.g. subset.data.frame and create your own subset version/method by making the trivial modification to that code. R is open source. Sometimes it's worthwhile to take advantage of this by making a simple modification to its code on your own before hunting for packages. Cheers, Bert Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Apr 8, 2018 at 9:06 PM, Sebastien Bihorel <[hidden email]> wrote: > Hi, > > The help page for subset states "subset: logical expression indicating elements or rows to keep: missing values are taken as false." > > Before I try to re-invent the wheel, I would like to know if one of the base or recommended packages would contain a variant of the subset function that would consider missing values as true. > > Thanks > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
In reply to this post by sbihorel
Sent from my iPhone > On Apr 8, 2018, at 9:06 PM, Sebastien Bihorel <[hidden email]> wrote: > > Hi, > > The help page for subset states "subset: logical expression indicating elements or rows to keep: missing values are taken as false." > > Before I try to re-invent the wheel, I would like to know if one of the base or recommended packages would contain a variant of the subset function that would consider missing values as true. > Just use a Boolean expression is.na(col)|(col==0) > Thanks > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
In reply to this post by sbihorel
Thanks. That works great! > df <- data.frame(x=c(1,1,NA,NA,2), y=c('a','a','a','b','b'), z=c(TRUE,FALSE,TRUE,FALSE,TRUE)) > cond1 <- 'x==1' > cond2 <- 'x==1 & z' > df x y z 1 1 a TRUE 2 1 a FALSE 3 NA a TRUE 4 NA b FALSE 5 2 b TRUE > subset(df, subset = ifelse(is.na(eval(parse(text=cond1))), TRUE, eval(parse(text=cond1)))) x y z 1 1 a TRUE 2 1 a FALSE 3 NA a TRUE 4 NA b FALSE > subset(df, subset = ifelse(is.na(eval(parse(text=cond2))), TRUE, eval(parse(text=cond2)))) x y z 1 1 a TRUE 3 NA a TRUE ----- Original Message ----- From: "S Ellison" <[hidden email]> To: "Sebastien Bihorel" <[hidden email]> Sent: Monday, April 9, 2018 8:31:55 AM Subject: RE: Question about subset > Before I try to re-invent the wheel, I would like to know if one of the base or > recommended packages would contain a variant of the subset function that > would consider missing values as true. The subset argument to subset is something that evaluates to logical - a vector of True or False. If you wanted to treat missing values _in that_ as TRUE, wrap it in an ifelse: ifelse(is.na(condition), TRUE, condition) where condition can be a logical or an expression evaluating to logical. S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}} ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
In reply to this post by David Winsemius
Thanks. S. Elison provided a similar but apparently more general solution (see other post in thread). ----- Original Message ----- From: "David Winsemius" <[hidden email]> To: "Sebastien Bihorel" <[hidden email]> Cc: [hidden email] Sent: Monday, April 9, 2018 12:33:41 AM Subject: Re: [R] Question about subset Sent from my iPhone > On Apr 8, 2018, at 9:06 PM, Sebastien Bihorel <[hidden email]> wrote: > > Hi, > > The help page for subset states "subset: logical expression indicating elements or rows to keep: missing values are taken as false." > > Before I try to re-invent the wheel, I would like to know if one of the base or recommended packages would contain a variant of the subset function that would consider missing values as true. > Just use a Boolean expression is.na(col)|(col==0) > Thanks > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
Free forum by Nabble | Edit this page |