[英]Merge two data frames in R and find common values and non-matching values
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:
所以,例如,df1是:
Name Position location
francesca A 75
cristina B 36
And df2 is: 而df2是:
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: 我尝试过函数
match
或者:
subset(df1, location %in% df2)
But it does not work. 但它不起作用。
Could you please help me figure out how to do this? 你能帮我弄明白怎么做吗?
Try: 尝试:
df1$match <- match(df1$location, df2$location, nomatch=0)
This will add a column to df1 indicating which row in df2 matches it (considering only location as you specified). 这将向df1添加一列,指示df2中的哪一行与其匹配(仅考虑您指定的位置)。 If there are no matches, zero will be returned, so you get:
如果没有匹配项,则返回零,因此您得到:
> df1
Name Position location match
1 francesca A 75 1
2 cristina B 36 0
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.
我认为这些是独特的,因为你指定你的问题的方式,所以这不应该是一个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.