Dear list, Let's say we have a data frame as follows,>expand.grid(a=1:5,b=c(1,5,10,20),DV=c(0.1,0.2,0.3))->df1 >df1$DV<-rgamma(60,shape=10)columns a and b are two levels. DV is the column with values we are interested in. Then another data frame df2 with values as follows data.frame(a=c(2,2,3,4,5),b=c(5,10,1,5,20))->df2 Now I want to a subset of df1 that match df2 at the both columns a and b. Any idea? Thanks. Jun [[alternative HTML version deleted]]
Hi Jun, I think merge(df1, df2) should do it. Best, Ista On Tue, Aug 14, 2012 at 3:05 PM, Jun Shen <jun.shen.ut at gmail.com> wrote:> Dear list, > > Let's say we have a data frame as follows, > >>expand.grid(a=1:5,b=c(1,5,10,20),DV=c(0.1,0.2,0.3))->df1 >>df1$DV<-rgamma(60,shape=10) > > columns a and b are two levels. DV is the column with values we are > interested in. > > Then another data frame df2 with values as follows > > data.frame(a=c(2,2,3,4,5),b=c(5,10,1,5,20))->df2 > > Now I want to a subset of df1 that match df2 at the both columns a and b. > Any idea? > > Thanks. > > Jun > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Rui Barradas
2012-Aug-14 19:13 UTC
[R] Subsetting rows by multiple levels of selected values
Hello, Try the following. df3 <- merge(df2, df1) Hope this helps, Rui Barradas Em 14-08-2012 20:05, Jun Shen escreveu:> Dear list, > > Let's say we have a data frame as follows, > >> expand.grid(a=1:5,b=c(1,5,10,20),DV=c(0.1,0.2,0.3))->df1 >> df1$DV<-rgamma(60,shape=10) > columns a and b are two levels. DV is the column with values we are > interested in. > > Then another data frame df2 with values as follows > > data.frame(a=c(2,2,3,4,5),b=c(5,10,1,5,20))->df2 > > Now I want to a subset of df1 that match df2 at the both columns a and b. > Any idea? > > Thanks. > > Jun > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
nalluri pratap
2012-Aug-14 21:42 UTC
[R] Subsetting rows by multiple levels of selected values
merge(df1,df2) --- On Wed, 15/8/12, Jun Shen <jun.shen.ut@gmail.com> wrote: From: Jun Shen <jun.shen.ut@gmail.com> Subject: [R] Subsetting rows by multiple levels of selected values To: "R-help" <r-help@stat.math.ethz.ch> Date: Wednesday, 15 August, 2012, 12:35 AM Dear list, Let's say we have a data frame as follows,>expand.grid(a=1:5,b=c(1,5,10,20),DV=c(0.1,0.2,0.3))->df1 >df1$DV<-rgamma(60,shape=10)columns a and b are two levels. DV is the column with values we are interested in. Then another data frame df2 with values as follows data.frame(a=c(2,2,3,4,5),b=c(5,10,1,5,20))->df2 Now I want to a subset of df1 that match df2 at the both columns a and b. Any idea? Thanks. Jun [[alternative HTML version deleted]] ______________________________________________ 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. [[alternative HTML version deleted]]
HI, In addition to merge(), you can also use:?join() library(plyr) join(df1,df2,type="inner") A.K. ----- Original Message ----- From: Jun Shen <jun.shen.ut at gmail.com> To: R-help <r-help at stat.math.ethz.ch> Cc: Sent: Tuesday, August 14, 2012 3:05 PM Subject: [R] Subsetting rows by multiple levels of selected values Dear list, Let's say we have a data frame as follows,>expand.grid(a=1:5,b=c(1,5,10,20),DV=c(0.1,0.2,0.3))->df1 >df1$DV<-rgamma(60,shape=10)columns a and b are two levels. DV is the column with values we are interested in. Then another data frame df2 with values as follows data.frame(a=c(2,2,3,4,5),b=c(5,10,1,5,20))->df2 Now I want to a subset of df1 that match df2 at the both columns a and b. Any idea? Thanks. Jun ??? [[alternative HTML version deleted]] ______________________________________________ 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.
Reasonably Related Threads
- Skipping panels in Lattice
- failed when merging two dataframes, why
- dataframes with only one variable
- merging or joining 2 dataframes: merge, rbind.fill, etc.?
- Best way/practice to create a new data frame from two given ones with last column computed from the two data frames?