[英]how to create data frame from nested list in R?
我想从嵌套列表中创建 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"
如何在保留列名的同时创建数据框?
这个怎么样?
样本数据
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
调用 data.frame() 时禁用名称检查
flatten_list <- lapply(tmp_list, data.frame, stringsAsFactors = FALSE, check.names = FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.