首页 > 解决方案 > 如何在 R 中展平深度且不规则嵌套的列表/数据框/JSON

问题描述

我想将 json 文件转换为数据框(数据框内没有任何嵌套列表)

temp <- jsonlite::fromJSON(txt ="https://unstats.un.org/SDGAPI/v1/sdg/GeoArea/Tree")

标签: rpurrrjsonlite

解决方案


递归取消嵌套:

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, ...

推荐阅读