【发布时间】:2015-04-06 13:53:21
【问题描述】:
样本数据:
l <- list(x=data.frame(X1=1,X2=2,X3=3,X4=4,X5=5),
y=data.frame(X1=6,X8=7,X4=8,X9=9,X5=10),
z=data.frame(X1=11,X2=12,X3=13,X4=14,X5=15)
)
我想rbind这个列表基于预先指定的列名,以便列名(和它的列位置匹配)。
# these are pre-defined columns names we want to `rbind` if no match, exclude the list entry
col <- c("X1","X2","X3","X4","X5")
所需的输出应该是data.frame:
X1 X2 X3 X4 X5
1 2 3 4 5
11 12 13 14 15
编辑:可能是这样的:
do.call(rbind, lapply(l, function(x) x[!any(is.na(match(c("X1","X2","X3","X4","X5"), names(x))))]))
【问题讨论】:
-
能有列名
c(col, somethingElse)的条目吗?即具有冗余列的条目。 -
@朱利叶斯,不,不可能。必须完全排除不匹配的列表。
标签: r