Map two data.frames files based on multiple columns

My head stands still. I would like to match / extract data from a larger data.frame (df) file based on columns in a smaller data.frame (mdf) format. What I'm stuck with is the fact that I want to match multiple columns (two in this case). I have tried different approaches, using, for example, merge, which, match %in%, but failed.

# Dummy example

# Large df
df <- mtcars[1:6,1:3]
df$car_1 <- rownames(df)
df$car_2 <- rownames(tail(mtcars))

# df to match
mdf <- df[c("car_1","car_2")][3:6,]

rownames(df) <- NULL
rownames(mdf) <- NULL

The desired result will look something like this:

 mpg cyl disp             car_1          car_2
22.8   4  108        Datsun 710 Ford Pantera L
21.4   6  258    Hornet 4 Drive   Ferrari Dino  
18.7   8  360 Hornet Sportabout  Maserati Bora
18.1   6  225           Valiant     Volvo 142E

It seems like it should be very straightforward.

Any pointer would be highly appreciated, thanks!

+8
source share
3 answers

How about merge(df, mdf, all.x = FALSE, all.y = TRUE)?

: , , , :

names(mdf) <- c("car_3", "car_4")
merge(df, mdf, by.x = c("car_1", "car_2"), by.y = c("car_3", "car_4"), 
      all.x = FALSE, all.y = TRUE)
+8

:

library(dplyr)
inner_join(df, mdf)

#Joining by: c("car_1", "car_2")
#              car_1          car_2  mpg cyl disp
#1        Datsun 710 Ford Pantera L 22.8   4  108
#2    Hornet 4 Drive   Ferrari Dino 21.4   6  258
#3 Hornet Sportabout  Maserati Bora 18.7   8  360
#4           Valiant     Volvo 142E 18.1   6  225
+6

If you want to use match or% in% for multiple columns, you can use interaction , insert, or use a list to match across multiple columns.

df[match(interaction(mdf), interaction(df[c("car_1", "car_2")])),]

df[match(paste(mdf$car_1, mdf$car_2), paste(df$car_1, df$car_2),),]

df[match(split(mdf, seq_len(nrow(mdf)))
         , split(df[c("car_1", "car_2")], seq_len(nrow(df)))),]

df[interaction(df[c("car_1", "car_2")]) %in% interaction(mdf),]
+1
source

All Articles