r - 如何在 R 中展平深度且不规则嵌套的列表/数据框/JSON
问题描述
我想将 json 文件转换为数据框(数据框内没有任何嵌套列表)
temp <- jsonlite::fromJSON(txt ="https://unstats.un.org/SDGAPI/v1/sdg/GeoArea/Tree")
解决方案
递归取消嵌套:
library(tidyr)
library(purrr)
library(dplyr)
recursive_unnest <- function(.data) {
# Exit condition: no more 'children' list-column
if (!"children" %in% names(.data) || !is.list(.data[["children"]])) return(.data)
x <- unnest(.data)
# Minor clean-up to make unnest work: replace NULLs with empty data frames
x <- mutate_if(x, is.list,
~ map_if(.x, ~ is.null(.x) || identical(.x, list()), ~ data.frame(geoAreaCode = NA)))
Recall(x)
}
recursive_unnest(temp)
# Observations: 489
# Variables: 16
# $ geoAreaCode <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
# $ geoAreaName <chr> "World", "World", "World", "World", "World", "World", "World", "Worl...
# $ type <chr> "Region", "Region", "Region", "Region", "Region", "Region", "Region"...
# $ geoAreaCode1 <int> 10, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2...
# $ geoAreaName1 <chr> "Antarctica", "Africa", "Africa", "Africa", "Africa", "Africa", "Afr...
# $ type1 <chr> "Country", "Region", "Region", "Region", "Region", "Region", "Region...
# $ geoAreaCode2 <int> NA, 15, 15, 15, 15, 15, 15, 15, 202, 202, 202, 202, 202, 202, 202, 2...
# $ geoAreaName2 <chr> NA, "Northern Africa", "Northern Africa", "Northern Africa", "Northe...
# $ type2 <chr> NA, "Region", "Region", "Region", "Region", "Region", "Region", "Reg...
# $ geoAreaCode3 <int> NA, 12, 818, 434, 504, 729, 788, 732, 14, 14, 14, 14, 14, 14, 14, 14...
# $ geoAreaName3 <chr> NA, "Algeria", "Egypt", "Libya", "Morocco", "Sudan", "Tunisia", "Wes...
# $ type3 <chr> NA, "Country", "Country", "Country", "Country", "Country", "Country"...
# $ geoAreaCode4 <int> NA, NA, NA, NA, NA, NA, NA, NA, 86, 108, 174, 262, 232, 231, 260, 40...
# $ geoAreaName4 <chr> NA, NA, NA, NA, NA, NA, NA, NA, "British Indian Ocean Territory", "B...
# $ type4 <chr> NA, NA, NA, NA, NA, NA, NA, NA, "Country", "Country", "Country", "Co...
# $ children <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
推荐阅读
- python - 如何在pyqt5中添加一个QInputDialog并获取它的值
- r - R data.table - 插值
- tensorflow - 为什么`tf.train.Optimizer().compute_gradients(loss)`也返回不在`loss`子图中的变量?
- linux - Android Studio 3.2.1 - 无法将项目与 gradle 文件同步:@NotNull 参数“消息”的参数......不能为空
- r - 如何在不重复变量的情况下合并两个具有共享案例和变量的数据集?
- rest - 你知道 Nestjs BE nodejs 框架的任何 REST 生成器吗?
- docusignapi - 没有签名者姓名的 Docusign 信封创建
- sql - 从 SQL 查询中添加缺失的日期
- jupyter-notebook - 通过 Jupyter 内核(Jupyter Notebook 扩展)执行代码时等待内核准备就绪
- python - Celery-Progress-Bar 在 Django 中不起作用