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

ko

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

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

我的列表和数据框如下。

 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

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

R中的列表创建数据框

如何从具有两个因素的数据框中创建嵌套列表?

如何从python中的数据框创建列表

从长度不等的嵌套列表中创建熊猫数据框

嵌套列表中的数据框

如何在保留键和值的 R 数据框中取消嵌套列表?

如何在 r 中创建带有嵌套 forloops 的数据框

如何从包含 R 中数据框列表的列表创建多个图

如何在R中将嵌套列表转换为数据框?

数据框到字典,如 R 中的嵌套列表

从列表的嵌套字典创建数据框

从包含列表的嵌套字典创建数据框

从Pandas数据框创建嵌套列表

从R中的列表列表创建数据框

如何从R中的数据框创建直方图

如何创建从Python中的熊猫数据框嵌套的JSON文件?

如何从Python中的嵌套for循环创建数据框?

如何在数据框中创建映射到R中的列表的列?

在R中,如何从分组的数据框中创建命名列表?

如何从数据框创建列表?

熊猫-从字典中的嵌套键值和嵌套列表创建数据框

如果我创建的命名列表与数据框的列同名,如何在r中添加数据框的行?

R中如何获取包含列表中值的行并创建计数数据框

如何通过在数据框R中创建列值列表来对列值进行分组

在熊猫中,如何从词典列表中创建数据框?

如何从列表中更改 API 中的变量并创建数据框

从嵌套的模型列表中创建显示AICc值的数据框列表