多个列表(和多个data.frame)到data.frame中

时间:2017-02-15 16:54:46

标签: r list

这是我输入的摘录:多个data.frame的多个列表。

我需要将其转换为data.frame。我尝试了不同的解决方案(例如,ldply),结果是:

 ###################################################
 Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
 arguments imply differing number of rows: 1, 17, 0
 ###################################################

从我的输入中获取:

 str(post_ss)
 List of 10
  $ :List of 3
   ..$ post    :'data.frame':   1 obs. of  10 variables:
   .. ..$ from_id       : chr "164700433551414"
   .. ..$ from_name     : chr "Banco Santander España"
   .. ..$ message       : chr "¿Estás buscando un entrenamiento personalizado? Descubre el que mejor se adapta a ti: http://bsan.es/1WaJ74l"
   .. ..$ created_time  : chr "2017-02-07T17:35:08+0000"
   .. ..$ type          : chr "link"
   .. ..$ link          : chr "http://bsan.es/1WaJ74l"
   .. ..$ id            : chr "164700433551414_1368700789818033"
   .. ..$ likes_count   : num 17
   .. ..$ comments_count: num 4
   .. ..$ shares_count  : num 2
   ..$ likes   :'data.frame':   17 obs. of  2 variables:
   .. ..$ from_name: chr [1:17] "Juan Beas" "Carmen Lahuerta" "Paco Fernández" "Julio AN" ...
   .. ..$ from_id  : chr [1:17] "10210792506824372" "1248829061897208" "1808370209415556" "392482737753899" ...
   ..$ comments:'data.frame':   0 obs. of  6 variables:
   .. ..$ from_id     : logi(0) 
   .. ..$ from_name   : logi(0) 
   .. ..$ message     : logi(0) 
   .. ..$ created_time: logi(0) 
   .. ..$ likes_count : logi(0) 
   .. ..$ id          : logi(0) 
  $ :List of 3
   ..$ post    :'data.frame':   1 obs. of  10 variables:
   .. ..$ from_id       : chr "164700433551414"
   .. ..$ from_name     : chr "Banco Santander España"
   .. ..$ message       : chr "Ya tenemos al ganador de Los Lunes 1l2l3 de esta semana, ¡enhorabuena a Ana Isabel González Gutiérrez! ¡Disfruta tu premio! Gra"| __truncated__
   .. ..$ created_time  : chr "2017-02-08T14:20:22+0000"
   .. ..$ type          : chr "video"
   .. ..$ link          : chr "https://www.facebook.com/bancosantander/videos/1369514563069989/"
   .. ..$ id            : chr "164700433551414_1369514563069989"
   .. ..$ likes_count   : num 46
   .. ..$ comments_count: num 6
   .. ..$ shares_count  : num 2
   ..$ likes   :'data.frame':   25 obs. of  2 variables:

亲切的问候, majesus

1 个答案:

答案 0 :(得分:1)

我们可以使用tidyverse

执行此操作
library(tidyverse)
post_ss %>%
       map(bind_rows) %>%
       bind_rows()

或者@alistaire提到

post_ss %>%
       map_df(bind_rows)

或使用rbindlist

中的data.table
library(data.table)
rbindlist(lapply(post_ss, rbindlist, fill = TRUE), fill = TRUE)

数据

post_ss <- list(structure(list(post = structure(list(from_id = 164700433551414, 
    from_name = "Banco Santander España"), .Names = c("from_id", 
"from_name"), row.names = c(NA, -1L), class = "data.frame"), 
    likes = structure(list(from_name = c("Juan Beas", "Carmen Lahuerta"
    ), from_id = c(1434334, 14343343)), .Names = c("from_name", 
    "from_id"), row.names = c(NA, -2L), class = "data.frame"), 
    comments = structure(list(from_id = logical(0), from_name = logical(0), 
        message = logical(0), created_time = logical(0)), .Names = c("from_id", 
    "from_name", "message", "created_time"), row.names = integer(0), class = "data.frame")), 
  .Names = c("post", 
"likes", "comments")), structure(list(post = structure(list(from_id = 13347051414, 
    from_name = "Bajco España"), .Names = c("from_id", "from_name"
), row.names = c(NA, -1L), class = "data.frame"), likes = structure(list(
    from_name = c("Luan Beas", "Carmen Lahuerta"), from_id = c(1430432, 
    1434244)), .Names = c("from_name", "from_id"), row.names = c(NA, 
-2L), class = "data.frame"), comments = structure(list(from_id = logical(0), 
    from_name = logical(0), message = logical(0), created_time = logical(0)),
   .Names = c("from_id", 
"from_name", "message", "created_time"),
 row.names = integer(0), class = "data.frame")), .Names = c("post", 
"likes", "comments")))