【问题标题】:Unlist a list within a list of data.frames into a single data.frame将 data.frames 列表中的列表取消列出到单个 data.frame
【发布时间】: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) 均无济于事。

【问题讨论】:

    标签: r list dataframe


    【解决方案1】:

    我们可以试试

    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
    

    【讨论】:

      【解决方案2】:

      使用 包的另一种方式。

      library(tidyverse)
      
      dat %>% flatten() %>% 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
      

      【讨论】:

        猜你喜欢
        • 2019-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多