在 R 中相交然后更新

Intersect then update in R

如果我有 table 喜欢

table 1

id   name
1     A
2     D
3     B
4     C

table 2

id   name
2    A
3    A
4    D
5    D

table 3

id   name
1    D
3    B
4    B
5    D

如果 m1 <- table 1 且 m2 <- table2 且 m3 <- table3,我所做的是:

aPid <-Reduce(intersect, list(m1$id,m2$id,m3$id)) 

我想根据相交的 id 更新 tables,例如:

m11 <- applyIndexToDataFrame(m1, m1$id %in% aPid)
m22 <- applyIndexToDataFrame(m2, m2$id %in% aPid)
m33 <- applyIndexToDataFrame(m3, m3$id %in% aPid)

'applyIndexToDataFram' is not real function. I just made up

然后最后:

table 1

id   name
3     B
4     C

table 2

id   name
3     A
4     D

table 3

id   name
3     B
4     B

R 中有没有根据相交数据更新 tables 的函数?

你的"applyIndextoDataFrame"功能只需使用括号子设置符号即可实现:

m11 <- m1[m1$id %in% aPid,]

如果您愿意,您可以一次完成:

m11 <- m1[m1$id %in% Reduce(intersect, list(m1$id,m2$id,m3$id)),]