2015-10-14 72 views
0
mylist <- list(structure(c(1L, 1L, 2L, 2L, 2L, 2L, NA, NA), .Names = c("A", 
"B", "C", "D", "E", "F", "G", "H")), structure(c(1L, 1L, 1L, 
1L, 1L, 2L, 1L, NA), .Names = c("A", "B", "C", "D", "E", "F", 
"G", "H"))) 

mylist 
[[1]] 
A B C D E F G H 
1 1 2 2 2 2 NA NA 

[[2]] 
A B C D E F G H 
1 1 1 1 1 2 1 NA 

我有一個像上面的列表,我想將它摺疊成一個data.frame,這樣我可以集中每一個單獨列即df$Adf$B談到列表爲data.frame

> df$A 
[1] 1 1 

> df$B 
[1] 1 1 

> df$C 
[1] 2 1 

如此反覆

+0

我想你想'do.call(rbind,lapply(mylist,as.data.frame.list))' –

+0

@RichardScriven謝謝。這正是我想要的 – Adrian

+0

其實,有了這個,因爲你已經有了所有的數值,你可以使用'as.data.frame(do.call(rbind,mylist))' –

回答

0

你可以unlistsplit根據名字,像

temp <- unlist(mylist) 
res <- split(unname(temp), names(temp)) 

# res$A 
# [1] 1 1 
# res$B 
# [1] 1 1 
# res$C 
# [1] 2 1