我有一个像
这样的列表的例子lst<-list(setNames(c(1,10,50,60,70,80),c("id","id1","math","phy","che","bio")),setNames(c(2,20,45),c("id","id1","phy")),setNames(c(3,30,75),c("id","id1","bio")))
我的预期结果:
df<-rbind(c(1,10,50,60,70,80),c(2,20,NA,45,NA,NA),c(3,30,NA,NA,NA,75))
colnames(df)<-c("id","id1","math","phy","che","bio")
df
任何建议将不胜感激。 提前谢谢。
答案 0 :(得分:1)
您可以在&#39; data.table&#39;的开发版中试用rbindlist
。即v1.9.5。它可以从here
library(data.table) #v1.9.5+
rbindlist(lapply(lst, as.data.frame.list), fill=TRUE)
# id id1 math phy che bio
#1: 1 10 50 60 70 80
#2: 2 20 NA 45 NA NA
#3: 3 30 NA NA NA 75
答案 1 :(得分:0)
另一种方式:
nms = unique(unlist(lapply(lst, names)))
do.call(rbind, lapply(lst, "[", nms))
# id id1 math phy che bio
#[1,] 1 10 50 60 70 80
#[2,] 2 20 NA 45 NA NA
#[3,] 3 30 NA NA NA 75
答案 2 :(得分:-1)
另一种方法:
上述问题的基础R解决方案
dataFrame <- do.call(rbind,List)
OR dplyr解决方案
library(dplyr)
dataFrame <- rbind_all(List)