I'm trying to compare two dataframes with the exact same amount of rows and variables, for changes in variable value per unique ID (returning True if the value is the same, and false if it is different). Here's an example of how the data looks:
我试图将两个dataframes与相同数量的行和变量进行比较,以确定每个惟一ID的变量值的变化(如果值相同,则返回True;如果值不同,则返回false)。这里有一个数据的例子:
df1
df1
id col1 col2
1 abc 123
2 def 456
3 ghi 789
df2
df2
col1 id col2
ghe 3 789
abc 1 123
def 2 455
And I guess I would have the result of the comparison be in df3
我想这个比较的结果应该是df3
id col1 col2
1 true true
2 true false
3 false true
Any help would be greatly appreciated! Hopefully I've made this somewhat clear.
如有任何帮助,我们将不胜感激!希望我已经讲得很清楚了。
1 个解决方案
#1
3
Try this:
试试这个:
cbind.data.frame(id=df1$id, df1[-1]==df2[match(df1$id, df2$id), names(df1)[-1]])
# id col1 col2
#1 1 TRUE TRUE
#2 2 TRUE FALSE
#3 3 FALSE TRUE
#1
3
Try this:
试试这个:
cbind.data.frame(id=df1$id, df1[-1]==df2[match(df1$id, df2$id), names(df1)[-1]])
# id col1 col2
#1 1 TRUE TRUE
#2 2 TRUE FALSE
#3 3 FALSE TRUE