Dimitri Liakhovitski
2009-Sep-24 22:52 UTC
[R] keeping all rows with the same values, and not only unique ones
Dear R-ers, I have a data frame "test": test<-data.frame(x=c(1,2,3,4,5,6,7,8),y=c(2,3,4,5,6,7,8,9),total=c(7,7,8,8,9,9,10,10)) test I have a vector "needed": needed<-c(7,9) needed I need the result to look like this: 1 2 7 2 3 7 5 6 9 6 7 9 When I do the following: result<-test[test["total"]==needed,] result I only get unique rows that have 7 or 9 in "total": 1 2 7 6 7 9 How could I keep ALL rows that have 7 or 9 in "total" Thanks a million! -- Dimitri Liakhovitski Ninah.com Dimitri.Liakhovitski at ninah.com
Phil Spector
2009-Sep-24 22:59 UTC
[R] keeping all rows with the same values, and not only unique ones
Dimitri - Use %in% instead of ==:> test[test$total %in% needed,]x y total 1 1 2 7 2 2 3 7 5 5 6 9 6 6 7 9 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Thu, 24 Sep 2009, Dimitri Liakhovitski wrote:> Dear R-ers, > > I have a data frame "test": > test<-data.frame(x=c(1,2,3,4,5,6,7,8),y=c(2,3,4,5,6,7,8,9),total=c(7,7,8,8,9,9,10,10)) > test > > I have a vector "needed": > needed<-c(7,9) > needed > > I need the result to look like this: > 1 2 7 > 2 3 7 > 5 6 9 > 6 7 9 > > When I do the following: > result<-test[test["total"]==needed,] > result > > I only get unique rows that have 7 or 9 in "total": > 1 2 7 > 6 7 9 > > How could I keep ALL rows that have 7 or 9 in "total" > > Thanks a million! > > -- > Dimitri Liakhovitski > Ninah.com > Dimitri.Liakhovitski at ninah.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. >
Moshe Olshansky
2009-Sep-24 23:46 UTC
[R] keeping all rows with the same values, and not only unique ones
test[which(test[,"total"] %in% needed),] --- On Fri, 25/9/09, Dimitri Liakhovitski <ld7631 at gmail.com> wrote:> From: Dimitri Liakhovitski <ld7631 at gmail.com> > Subject: [R] keeping all rows with the same values, and not only unique ones > To: "R-Help List" <r-help at stat.math.ethz.ch> > Received: Friday, 25 September, 2009, 8:52 AM > Dear R-ers, > > I have a data frame "test": > test<-data.frame(x=c(1,2,3,4,5,6,7,8),y=c(2,3,4,5,6,7,8,9),total=c(7,7,8,8,9,9,10,10)) > test > > I have a vector "needed": > needed<-c(7,9) > needed > > I need the result to look like this: > 1 2 7 > 2 3 7 > 5 6 9 > 6 7 9 > > When I do the following: > result<-test[test["total"]==needed,] > result > > I only get unique rows that have 7 or 9 in "total": > 1 2 7 > 6 7 9 > > How could I keep ALL rows that have 7 or 9 in "total" > > Thanks a million! > > -- > Dimitri Liakhovitski > Ninah.com > Dimitri.Liakhovitski at ninah.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. >
Dimitri Liakhovitski
2009-Sep-25 12:56 UTC
[R] keeping all rows with the same values, and not only unique ones
Thank you so much, everyone! Very helpful! Dimitri On Thu, Sep 24, 2009 at 7:46 PM, Moshe Olshansky <m_olshansky at yahoo.com> wrote:> test[which(test[,"total"] %in% needed),] > > --- On Fri, 25/9/09, Dimitri Liakhovitski <ld7631 at gmail.com> wrote: > >> From: Dimitri Liakhovitski <ld7631 at gmail.com> >> Subject: [R] keeping all rows with the same values, and not only unique ones >> To: "R-Help List" <r-help at stat.math.ethz.ch> >> Received: Friday, 25 September, 2009, 8:52 AM >> Dear R-ers, >> >> I have a data frame "test": >> test<-data.frame(x=c(1,2,3,4,5,6,7,8),y=c(2,3,4,5,6,7,8,9),total=c(7,7,8,8,9,9,10,10)) >> test >> >> I have a vector "needed": >> needed<-c(7,9) >> needed >> >> I need the result to look like this: >> 1 2 7 >> 2 3 7 >> 5 6 9 >> 6 7 9 >> >> When I do the following: >> result<-test[test["total"]==needed,] >> result >> >> I only get unique rows that have 7 or 9 in "total": >> 1 2 7 >> 6 7 9 >> >> How could I keep ALL rows that have 7 or 9 in "total" >> >> Thanks a million! >> >> -- >> Dimitri Liakhovitski >> Ninah.com >> Dimitri.Liakhovitski at ninah.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. >> >-- Dimitri Liakhovitski Ninah.com Dimitri.Liakhovitski at ninah.com