将data.frame列表中的列表取消列出为单个data.frame

阿德里安
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

我有一个名为data.frames的列表dat,其中每个列表元素中都有多个条目(例如,按性别)。如何将这个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)无济于事。

阿克伦

我们能试试

library(tidyverse)
map_df(dat, bind_rows)
# 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章