如何从R中的嵌套列表创建数据框?

时间:2020-07-29 08:46:06

标签: r list dataframe

我想从嵌套列表中创建数据框。

创建数据框时,列名消失。

我的列表和数据框如下。

 str(tmp_list)

 $ :List of 5
  ..$ _index : chr "test"
  ..$ _id    : chr "uuid1"
  ..$ _score : num 1
  ..$ _source:List of 4
  .. ..$ actor         :List of 1
  .. .. ..$ email: chr "test@gmail.com"
  .. ..$ result        :List of 1
  .. .. ..$ score: num 5
  .. ..$ @kst_timestamp: chr "2020-07-27T04:58:11.614Z"
  .. ..$ object        :List of 1
  .. .. ..$ extension:List of 1
  .. .. .. ..$ class:List of 1
  .. .. .. .. ..$ id: chr "class1"
 $ :List of 5
  ..$ _index : chr "test2"
  ..$ _id    : chr "uuid2"
  ..$ _score : num 1
  ..$ _source:List of 4
  .. ..$ actor         :List of 1
  .. .. ..$ email: chr "test2@gmail.com"
  .. ..$ result        :List of 1
  .. .. ..$ score: num 5
  .. ..$ @kst_timestamp: chr "2020-07-27T05:04:09.616Z"
  .. ..$ object        :List of 1
  .. .. ..$ extension:List of 1
  .. .. .. ..$ class:List of 1
  .. .. .. .. ..$ id: chr "class2"

我想像这样转换数据帧...

str(final_df)

'data.frame': 2 obs. of  7 variables:
 $ _index      : chr "test" "test2"
 $ _id      : chr "uuid1" "uuid2"
 $ _score      : num 1 1
 $ _source.actor.email      : chr "test@gmail.com" "test2@gmail.com"
 $ _source.result.score      : num 1 5
 $ _source.@kst_timestamp      : chr "2020-07-27T04:58:11.614Z" "2020-07-27T05:04:09.616Z"
 $ _source.object.extension.class.id        : chr  "class1" "class2"

这是我的消息来源...

flatten_list <- lapply(tmp_list, data.frame, stringsAsFactors = FALSE)
final_df <- do.call(rbind,flatten_list) %>% as.data.frame

str(final_df)
'data.frame': 2 obs. of  7 variables:
 $ X_index                : chr  "test" "test2"
 $ X_id                   : chr  "uuid1" "uuid2"
 $ X_score                : num  1 1 
 $ X_source.email          : chr  "test@gmail.com" "test2@gmail.com"
 $ X_source.score         : num  1 5 
 $ X_source..kst_timestamp: chr  "2020-07-27T04:58:11.614Z" "2020-07-27T05:04:09.616Z"
 $ X_source.id            : chr  chr  "class1" "class2"

如何在保留列名的同时创建数据框?

2 个答案:

答案 0 :(得分:0)

怎么样?

样本数据

x <- list(
  
  list(`_index` = "test" , `_id` = "uuid1", source = list(actor = list(email = "test@example.it"))),
  list(`_index` = "test2", `_id` = "uuid2", source = list(actor = list(email = "test2@example.it")))
  
)

解决方案:

library(purrr)
map_dfr(x, unlist)

# # A tibble: 2 x 3
#   `_index` `_id` source.actor.email
#   <chr>    <chr> <chr>             
# 1 test     uuid1 test@example.it   
# 2 test2    uuid2 test2@example.it  

答案 1 :(得分:0)

仅在调用data.frame()时禁用名称检查

flatten_list <- lapply(tmp_list, data.frame, stringsAsFactors = FALSE, check.names = FALSE)