我想从嵌套列表创建数据框。
当我创建数据框时,列名消失。
我的列表和数据框如下。
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 "[email protected]"
.. ..$ 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 "[email protected]"
.. ..$ 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 "[email protected]" "[email protected]"
$ _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 "[email protected]" "[email protected]"
$ 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 = "[email protected]"))),
list(`_index` = "test2", `_id` = "uuid2", source = list(actor = list(email = "[email protected]")))
)
解:
library(purrr)
map_dfr(x, unlist)
# # A tibble: 2 x 3
# `_index` `_id` source.actor.email
# <chr> <chr> <chr>
# 1 test uuid1 [email protected]
# 2 test2 uuid2 [email protected]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句