【发布时间】:2020-12-29 13:07:15
【问题描述】:
我在命名空间中有几个名为 a32、a33、...、a63 的数据帧,我必须将它们绑定到单个数据帧。每个都有几个(大约 20 个)列。他们应该有共同的列名,但不幸的是有一些列丢失了。当我尝试 rbind 时,这会导致错误。
l <- 32:63
l<- as.character(l) ## create a list of characters
A <- do.call(rbind.data.frame,mget(paste0("a",l))) ## "colnames not matching" error
Error in (function (..., deparse.level = 1, make.row.names = TRUE, stringsAsFactors = default.stringsAsFactors(), :
numbers of columns of arguments do not match
我想通过只使用公共列来绑定它们。我尝试在 for 循环中使用 paste0 来列出所有数据框的列名,并查看哪些数据框缺少列但无处可去。如何通过一一列出每个数据框的列名来避免手动搜索丢失的列。
举个小例子,说:
a32 <- data.frame(AB = 1, CD = 2, EF = 3, GH = 4)
a33 <- data.frame(AB = 6, EF = 7)
a34 <- data.frame(AB = 8, CD = 9, EF = 10, GH = 11)
a35 <- data.frame(AB = 12,CD = 13, GH = 14)
a36 <- data.frame(AB = 15,CD = 16,EF = 17,GH = 18)
and so on
有没有一种有效的方法来 rbind 命名空间中的所有 32 个数据帧?
【问题讨论】:
-
这能回答你的问题吗:stackoverflow.com/a/8605132/14425671
-
数据帧的数量很大,所以mget+Reduce+intersect对我来说似乎更有效。