Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - match two data.frames based on multiple columns

My head stands still at the moment. I would like to match/extract data from a larger data.frame (df) based on the columns in a smaller data.frame (mdf). What I'm getting stuck on is the fact that I want to match multiple columns (two in this case). I have tried different approaches using e.g. merge, which, match %in% but non have succeeded.

# 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 output would look something like

 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

This feels like it should be very straight forward.

Any pointer would be highly appreciated, thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Edit: If you have different column names you can specify which ones to merge on, e.g.:

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...