如果 names() 匹配,则 2 个列表之间的条件 rbind 元素;地图()?

Conditional rbind elements between 2 lists if their names() match; Map()?

我有 2 个要绑定的对象列表:

BothJoined <- Map(rbind, A, B)

但是,我想找到一种有条件的 rbind 方法,其中 rbind 发生在所有匹配 names()

的列表元素上

示例:

A <- list("a" = as.data.frame(50:55), "b" = as.data.frame(5:10),  "c" = as.data.frame(1:3))
colnames(A$a) <- "Col"
colnames(A$b) <- "Col"
colnames(A$c) <- "Col"

B <- list("a" = as.data.frame(55:60),  "c" = as.data.frame(4:6))
colnames(B$a) <- "Col"
colnames(B$c) <- "Col"

解决方案的外观:

BothJoined <- MapOnMatchingNames(A,B)

BothJoined$a
   Col
1   50
2   51
3   52
4   53
5   54
6   55
7   55
8   56
9   57
10  58
11  59
12  60

BothJoined$b
   Col
1   5
2   6
3   7
4   8
5   9
6   10

BothJoined$c
   Col
1   1
2   2
3   3
4   4
5   5
6   6

理想情况下,method/function 工作与每个列表元素中存储的 typeof() 对象无关。

您可以使用

非常有效地做到这一点
nms <- union(names(A), names(B))
l <- Map(rbind, A[nms], B[nms])
names(l) <- nms # needed if 'names(A)' does not contain 'names(B)'