在R中合并两个数据框并找到共同值和不匹配值
我试图找到一个函数来仅在一个公共列上匹配两个不同长度的数据帧,并创建一个不同的列来指定是否找到匹配项。 所以,举例来说, df1 是:
Name Position location
francesca A 75
cristina B 36
df2 是:
location Country
75 UK
56 Austria
我想匹配“位置”,输出如下:
Name Position Location Match
francesca A 75 1
cristina B 36 0
我尝试使用函数 match
或 with:
subset(df1, location %in% df2)
但它不起作用。
你能帮我弄清楚如何做到这一点吗?
I am trying to find a function to match two data frames of different lengths on one common column only, and create a different column which specifies if it found a match or not.
So, for example,
df1 is:
Name Position location
francesca A 75
cristina B 36
And df2 is:
location Country
75 UK
56 Austria
And I would like to match on "Location" and the output to be something like:
Name Position Location Match
francesca A 75 1
cristina B 36 0
I have tried with the function match
or with:
subset(df1, location %in% df2)
But it does not work.
Could you please help me figure out how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
这将向 df1 添加一列,指示 df2 中的哪一行与其匹配(仅考虑您指定的位置)。如果没有匹配项,将返回零,因此您会得到:
一个警告:如果第二个表中有多个匹配项,您需要使用不同的方法,因为此方法仅返回第一个匹配项。由于您指定问题的方式,我认为这些是独一无二的,所以这不应该成为问题。
Try:
This will add a column to df1 indicating which row in df2 matches it (considering only location as you specified). If there are no matches, zero will be returned, so you get:
One caution: if there are multiple matches in the second table, you'd want to use a different approach as this method only returns the first match. I assume these are unique because of the way you specified your question, so this should not be an issue.