根据多个列匹配两个data.frames [英] match two data.frames based on multiple columns

查看:58
本文介绍了根据多个列匹配两个data.frames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻我的头静止不动.我想基于较小的data.frame(mdf)中的列从较大的data.frame(df)中匹配/提取数据.我遇到的问题是我想匹配多个列(在这种情况下为两个).我尝试使用例如不同的方法mergewhichmatch %in%,但未成功.

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

所需的输出看起来像

 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

感觉应该很简单.

非常感谢任何指针,谢谢!

Any pointer would be highly appreciated, thanks!

推荐答案

merge(df, mdf, all.x = FALSE, all.y = TRUE)怎么样?

:如果您使用不同的列名,则可以指定要合并的列名,例如:

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)

这篇关于根据多个列匹配两个data.frames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆