Dear list: I have a problem using the subset function: dat<- data.frame(treatment=c("A", "B", "A", "C", "C", "D", "A", "D", "C", "D"), response=rnorm(10)) I am interested in treatments "A", "B" and "D" vec<- c("A", "B", "D") But I can only obtain what I want with: subset(dat, treatment=="A" | treatment=="B" | treatment=="D") What's wrong with subset(dat, treatment==vec) It would be much more simple to compute the latter in a rather complex dataframe. Thanks in advance Justin -- partofy at inoutbox.com --
Justin, try subset(dat, treatment %in% vec) I guess thats what you want. On Fri, May 2, 2008 at 8:40 PM, <partofy@inoutbox.com> wrote:> Dear list: > > I have a problem using the subset function: > > dat<- data.frame(treatment=c("A", "B", "A", "C", "C", "D", "A", "D", > "C", "D"), response=rnorm(10)) > > I am interested in treatments "A", "B" and "D" > vec<- c("A", "B", "D") > > But I can only obtain what I want with: > subset(dat, treatment=="A" | treatment=="B" | treatment=="D") > > What's wrong with > subset(dat, treatment==vec) > > It would be much more simple to compute the latter in a rather complex > dataframe. > Thanks in advance > Justin > -- > > partofy@inoutbox.com > > -- > > ______________________________________________ > R-help@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. >-- HTH/Best, Jim Porzak Responsys, Inc. San Francisco, CA http://www.linkedin.com/in/jimporzak [[alternative HTML version deleted]]
Try %in% subset(dat, treatment %in% vec)> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of > partofy at inoutbox.com > Sent: Friday, May 02, 2008 11:41 PM > To: r-help at r-project.org > Subject: [R] help with subset > > Dear list: > > I have a problem using the subset function: > > dat<- data.frame(treatment=c("A", "B", "A", "C", "C", "D", > "A", "D", "C", "D"), response=rnorm(10)) > > I am interested in treatments "A", "B" and "D" > vec<- c("A", "B", "D") > > But I can only obtain what I want with: > subset(dat, treatment=="A" | treatment=="B" | treatment=="D") > > What's wrong with > subset(dat, treatment==vec) > > It would be much more simple to compute the latter in a > rather complex dataframe. > Thanks in advance > Justin > -- > > partofy at inoutbox.com > > -- > > ______________________________________________ > 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. > >