【发布时间】:2019-01-02 17:08:40
【问题描述】:
我正在通过 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 会显示 2 个顶级列表项。使用循环的主要缺点是有时会丢失导致循环停止的值。我想将其移至purrr,但我正在努力使用at_depth 或modify_depth 提取第二级嵌套项。巢内还有巢,确实增加了复杂性。
最终状态应该是一个整洁的数据框 - 从这些数据中,df 将只有 2 行,但会有很多列,每列代表一个项目,无论该项目嵌套在此列表中的哪个位置。如果缺少某些东西,那么它应该是一个 NA 值。
一个解决方案的理想场景,即使它可能不优雅,每个级别/嵌套项目都有一个数据框,然后可以在以后绑定在一起。
谢谢。
【问题讨论】: