尽管有很多结果,但似乎没有人回答以下问题: 我有两个数据帧
df1 = data.frame(x= c("a1","b1","c1","d1","e1","f1"),y = c("a2","b2","c2","d2","e2","f2"), z = 1:6)
df2 = data.frame(x = c("a1", "b1"), y = c("a2", "b2"))
他们返回:
> df1
x y z
1 a1 a2 1
2 b1 b2 2
3 c1 c2 3
4 d1 d2 4
5 e1 e2 5
6 f1 f2 6
> df2
x y
1 a1 a2
2 b1 b2
我如何获得他们的其余内部联接,我的意思是我想要得到:
x y z
1 c1 c2 3
2 d1 d2 4
3 e1 e2 5
4 f1 f2 6
我尝试了
subset(df1, (df1$x %in% df2$x) & (df1$y %in% df2$y)
)
但它也不起作用,也尝试了左连接,但是我一直在获取原始数据帧df1
答案 0 :(得分:0)
这是一个解决方案,它使用将第一列和第二列组合在一起,然后使用%in%
函数组成的键。
df1 = data.frame(x= c("a1","b1","c1","d1","e1","f1"),y = c("a2","b2","c2","d2","e2","f2"), z = 1:6)
df2 = data.frame(x = c("a1", "b1"), y = c("a2", "b2"))
#create a unique key for each dataframe
df1key<-paste(df1$x, df1$y)
df2key<-paste(df2$x, df2$y)
#return rows not matching rows in df2
answer<-df1[!(df1key %in% df2key),]