开发者

Compare two data.frames to find the rows in data.frame 1 and data.frame 2 which have equal values in selected columns

开发者 https://www.devze.com 2023-04-01 04:13 出处:网络
I have 2 data frames (a1 and a2) a1 ABCD 1A68 2D73 #** 3X33 a2 ABCD 4D23 #** 5Z35 6X34 a1 <- data.frame( A = 1:3,

I have 2 data frames (a1 and 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)
)

I want to get the tuples (a1$A,a2$A) for the rows which have the开发者_如何学C same values in colums B and D

In this example, I would get (2,4) because they have the same values in colums B and D, respectively D and 3


Use merge to merge the data frames.

merged <- merge(a1, a2, c("B", "D"))
subset(merged, select = c(A.x, A.y))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号