r-从JSON生成的多级列表中提取数据帧,偶尔会丢失元素



我通过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_depthmodify_depth提取二级嵌套项。巢穴中也有巢穴,这确实增加了复杂性。

结束状态应该是一个整洁的数据帧——从这个数据中,df将只有2行,但将有许多列,每个列表示一个项目,无论该项目嵌套在该列表的何处。如果缺少某个内容,那么它应该是NA值。

解决方案的理想场景是,生成的每个级别/嵌套项都有一个数据帧,然后可以绑定在一起,尽管这可能很不雅。

谢谢。

步骤1:使用社区wiki的功能将NULL替换为NA

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)

步骤2:

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

最新更新