我有两个数据集,都包含" ID"变量。在这两个数据集中,如何保持两个数据集中存在ID的观察结果?我正在使用R.
如
df1 <- structure(list(CustomerId = c(1, 2, 3, 4, 5, 8, 9), Product = structure(c(4L,
4L, 4L, 3L, 3L, 1L, 2L), .Label = c("abc", "def", "Radio", "Toaster"
), class = "factor")), .Names = c("CustomerId", "Product"), row.names = c(NA,
-7L), class = "data.frame")
df2 <-
structure(list(CustomerId = c(2, 4, 6, 7), State = structure(c(2L,
2L, 3L, 1L), .Label = c("aaa", "Alabama", "Ohio"), class = "factor")), .Names = c("CustomerId",
"State"), row.names = c(NA, -4L), class = "data.frame")
在两个数据集中,我希望保留两个数据中存在的观察结果。 (两个数据集中的ID为2和4)。
答案 0 :(得分:0)
您可以使用像
这样的基本子集subset(df1, CustomerId %in% df2$CustomerId)
subset(df2, CustomerId %in% df1$CustomerId)
如果您使用dplyr
,则称为semi_join
library(dplyr)
semi_join(df1, df2)
semi_join(df2, df1)
答案 1 :(得分:0)
merge()将是最简单的解决方案之一。这是内连接等价,如果需要外连接,则检查其他参数。
merge(df1, df2, by="CustomerId")[,1:2]
CustomerId Product
1 2 Toaster
2 4 Radio
merge(df2, df1, by="CustomerId")[,1:2]
CustomerId State
1 2 Alabama
2 4 Alabama