I have two data frames, say df1 and df2. Now I want to subset df2 based on the matching of multiple columns between df1 and df2.
e.g
df1
A B #column names, rows in df1 are unique, A1,B1 etc are characters
A1 B1
A2 B2
......
df2
C D E F G
A1 B1 E1 F1 G1
A2 B2 E2 .......
A1 B2 E3 .......
A1 B1 E4 .......
A2 B1 E5 .......
Here I want to match columns A and B in df1 to columns C and D in df2,and construct a new data frame df3, where each row of df3 store the row index of df2 where matching happens. For my example, it should be
df3
c(1,4)
c(2)
Originally I am thinking of paste characters and do string comparison to do the matching, but I suspect this is not the efficient way to do this, any better idea?
sapply(seq(nrow(df1)) , function(i) which( match(paste0(df2$C,df2$D) , paste0(df1$A,df1$B)) %in% i))- user20650