简体   繁体   中英

Turning list into a data.frame

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 

I have a list like above and I want to collapse it into a data.frame so that I can subset each column individually ie df$A , df$B , etc.

> df$A
[1] 1 1

> df$B
[1] 1 1

> df$C
[1] 2 1

And so forth

You could unlist and the split according to the names, something like

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM