代码之家  ›  专栏  ›  技术社区  ›  DeduciveR

从JSON生成的包含偶尔缺少元素的多级列表中提取到数据帧

  •  0
  • DeduciveR  · 技术社区  · 6 年前

    我正在通过一个API提取足球数据-结果JSON作为一个列表返回; dput 下面的例子:

    list(list(id = 10332894L, league_id = 8L, season_id = 12962L, 
    aggregate_id = NULL, venue_id = 201L, localteam_id = 51L, 
    visitorteam_id = 27L, weather_report = list(code = "drizzle", 
        temperature = list(temp = 53.92, unit = "fahrenheit"), 
        clouds = "90%", humidity = "87%", wind = list(speed = "12.75 m/s", 
            degree = 200L)), attendance = 25098L, leg = "1/1", 
    deleted = FALSE, referee = list(data = list(id = 15267L, 
        common_name = "L. Probert", fullname = "Lee Probert", 
        firstname = "Lee", lastname = "Probert"))), list(id = 10332895L, 
    league_id = 8L, season_id = 12962L, aggregate_id = NULL, 
    venue_id = 340L, localteam_id = 251L, visitorteam_id = 78L, 
    weather_report = list(code = "drizzle", temperature = list(
        temp = 50.07, unit = "fahrenheit"), clouds = "90%", humidity = "93%", 
        wind = list(speed = "6.93 m/s", degree = 160L)), attendance = 22973L, 
    leg = "1/1", deleted = FALSE, referee = list(data = list(
        id = 15273L, common_name = "M. Oliver", fullname = "Michael Oliver", 
        firstname = "Michael", lastname = "Oliver"))))
    

    我现在正在使用for循环提取-当完整数据中有数百个数据时,reprex会显示两个顶级列表项。使用循环的主要缺点是有时会丢失导致循环停止的值。我想把这个搬到 purrr 但我仍在努力使用 at_depth modify_depth . 嵌套中也有嵌套,这确实增加了复杂性。

    结束状态应该是一个整洁的数据框架——从这个数据来看,df将只有2行,但是将有许多列,每个列代表一个项目,不管该项目嵌套在这个列表中的什么地方。如果有东西不见了,那应该是 NA 价值。

    一个解决方案的理想方案是,即使它可能不太好,每个级别/生成的嵌套项都有一个数据帧,然后可以绑定在一起。

    谢谢。

    1 回复  |  直到 6 年前
        1
  •  1
  •   A. Suliman    6 年前

    步骤1:替换 NULL 具有 NA 使用社区wiki功能 here

    simple_rapply <- function(x, fn)
    {
      if(is.list(x))
      {
        lapply(x, simple_rapply, fn)
      } else
      {
        fn(x)
      }
    }    
    non.null.l <- simple_rapply(l, function(x) if(is.null(x)) NA else x)
    

    第二步:

    library(purrr)
    map_df(map(non.null.l,unlist),bind_rows)