【发布时间】:2020-11-10 12:53:59
【问题描述】:
我正在尝试对 r 中多个数据帧中的值的匹配组合进行子集化。
我的第一个数据集是这样的
head(df_1)
BCluster ACluster Total_tim ttransfer
1 0 0 955. 1
2 0 15 3060 0
3 0 36 2433 1
4 0 47 2518. 1.5
5 0 51 1122. 0.15
6 0 67 750 1
我的第二个数据集是这样的
head(df_2)
BCluster ACluster Total_tim ttransfer
1 5 5 1739. 0
2 5 11 2842 0
3 5 12 4661 0
4 5 27 2913 0
5 5 29 3748. 0
6 5 42 2035 0
第三个是这样的
head(df_3)
BCluster ACluster Total_tim ttransfer
1 0 0 6544 2
2 0 11 2834 1
3 0 15 2159 2
4 0 24 4658 1
5 0 29 5740. 1
6 0 31 2724 2
所有三个数据集都是 ID 列“Bcluster”和“Acluster”的组合。 对于这三个数据帧,我想保留 Bcluster 和 Acluster 的重叠组合 删除每个数据框并删除其他数据框
例如,因为在 df_2 中没有 Bcluster=0 和 Acluster=0 的组合。 Bcluster=0 和 Acluster=0 组合仅出现在 df_1 和 df_3 的第一行,因此我希望将它们全部从每个数据帧中删除。
我希望有人能帮我编写这个逻辑。
样本数据
dput(head(df_1))
structure(list(BCluster = c(0L, 0L, 0L, 0L, 0L, 0L), ACluster = c(0L,
15L, 36L, 47L, 51L, 67L), Total_tim = c(955.25, 3060, 2433, 2518.5,
1122.4, 750), ttransfer = c(1, 0, 1, 1.5, 0.15, 1)), row.names = c(NA,
-6L), groups = structure(list(BCluster = 0L, .rows = structure(list(
1:6), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr",
"list"))), row.names = 1L, class = c("tbl_df", "tbl", "data.frame"
), .drop = TRUE), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
))
dput(head(df_2))
structure(list(BCluster = c(5L, 5L, 5L, 5L, 5L, 5L), ACluster = c(5L,
11L, 12L, 27L, 29L, 42L), Total_tim = c(1739.31818181818, 2842,
4661, 2913, 3748.33333333333, 2035), ttransfer = c(0, 0, 0, 0,
0, 0)), row.names = c(NA, -6L), groups = structure(list(BCluster = 5L,
.rows = structure(list(1:6), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1L, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
> dput(head(df_3))
structure(list(BCluster = c(0L, 0L, 0L, 0L, 0L, 0L), ACluster = c(0L,
11L, 15L, 24L, 29L, 31L), Total_tim = c(6544, 2834, 2159, 4658,
5739.5, 2724), ttransfer = c(2, 1, 2, 1, 1, 2)), row.names = c(NA,
-6L), groups = structure(list(BCluster = 0L, .rows = structure(list(
1:6), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr",
"list"))), row.names = 1L, class = c("tbl_df", "tbl", "data.frame"
), .drop = TRUE), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
))
【问题讨论】:
-
您可以使用 dput() 函数为 3 个数据帧共享示例数据吗?
-
@KarthikS 当然!!我已按问题编辑。谢谢
-
我认为的解决方案是生成 Bcluster 和 Acluster 的所有组合。 Bcluster 和 Acluster 列只是地点的 ID。我有 47304 个 ID,所以我无法生成 47304*47304 的组合。实际上每个数据框并没有使用所有的组合。
-
因此,在您的样本数据中,没有任何匹配项,正如您提到的,您需要在 BCluster 和 Acluster 上都进行匹配,在这种情况下可能很难看到输出。
-
@KarthikS 这是一个相当大的数据框,我无法使用 dput() 添加数据。它们分别是 4 乘 199371、299197、74052。有什么建议么?所以我的最终输出可能会小于 74052*4 数据框