【发布时间】:2018-05-18 19:09:37
【问题描述】:
dat <- list(list(structure(list(ID = 1, Gender = structure(1L, .Label = "Male", class = "factor"),
Phrase = structure(1L, .Label = "Hello", class = "factor"),
Phrase2 = structure(1L, .Label = "Goodbye", class = "factor")), .Names = c("ID",
"Gender", "Phrase", "Phrase2"), row.names = c(NA, -1L), class = "data.frame"),
structure(list(ID = 1, Gender = structure(1L, .Label = "Female", class = "factor"),
Phrase = structure(1L, .Label = "Good afternoon", class = "factor"),
Phrase2 = structure(1L, .Label = "Goodbye", class = "factor")), .Names = c("ID",
"Gender", "Phrase", "Phrase2"), row.names = c(NA, -1L), class = "data.frame")),
list(structure(list(ID = 2, Gender = structure(1L, .Label = "Male", class = "factor"),
Phrase = structure(1L, .Label = "Hello", class = "factor"),
Phrase2 = structure(1L, .Label = "Good afternoon", class = "factor")), .Names = c("ID",
"Gender", "Phrase", "Phrase2"), row.names = c(NA, -1L), class = "data.frame"),
structure(list(ID = 2, Gender = structure(1L, .Label = "Female", class = "factor"),
Phrase = structure(1L, .Label = "Goodbye", class = "factor"),
Phrase2 = structure(1L, .Label = "Goodbye", class = "factor")), .Names = c("ID",
"Gender", "Phrase", "Phrase2"), row.names = c(NA, -1L
), class = "data.frame")))
> dat
[[1]]
[[1]][[1]]
ID Gender Phrase Phrase2
1 1 Male Hello Goodbye
[[1]][[2]]
ID Gender Phrase Phrase2
1 1 Female Good afternoon Goodbye
[[2]]
[[2]][[1]]
ID Gender Phrase Phrase2
1 2 Male Hello Good afternoon
[[2]][[2]]
ID Gender Phrase Phrase2
1 2 Female Goodbye Goodbye
我有一个名为 dat 的 data.frames 列表,其中每个列表元素中都有多个条目(例如按性别)。如何将这个 data.frames 列表组合成一个看起来像这样的 data.frame?
ID Gender Phrase Phrase2
1 1 Male Hello Goodbye
2 1 Female Good afternoon Goodbye
3 2 Male Hello Good afternoon
4 2 Female Goodbye Goodbye
我尝试过ldply(dat, data.frame)、do.call("rbind", dat) 和rbind.fill(dat) 均无济于事。
【问题讨论】: