我有2个数据框(a1和a2)
a1
A B C D
1 A 6 8
2 D 7 3 #**
3 X 3 3
a2
A B C D
4 D 2 3 #**
5 Z 3 5
6 X 3 4
a1 <- data.frame(
A = 1:3,
B = c("A", "D", "X"),
C = c(6, 7, 3),
D = c(8, 3, 3)
)
a2 <- data.frame(
A = 4:6,
B = c("D", "Z", "X"),
C = c(2, 3, 3),
D = c(3, 5, 4)
)
我想获得在列B和D中具有相同值的行的元组(a1 $ A,a2 $ A)
在这个例子中,我会得到 (2,4)因为它们在列B和D中分别具有相同的值,分别为D和3
答案 0 :(得分:3)
使用merge
合并数据框。
merged <- merge(a1, a2, c("B", "D"))
subset(merged, select = c(A.x, A.y))