我从API提取了一份数据。数据具有多个嵌套列表。什么是清除此数据的有效方法?
作为参考,我正在尝试关注parsing json with purrr上的这篇文章,但是看来我的数据具有更多的嵌套列表,所以我在使用它时遇到了一些困难。
> jsonRespParsed %>% dput()
list(list(GameId = 14491L, Season = 2019L, SeasonType = 3L, Day = "2019-04-14T00:00:00",
DateTime = "2019-04-14T12:00:00", Status = "Final", AwayTeamId = 11L,
HomeTeamId = 14L, AwayTeamName = "NYI", HomeTeamName = "PIT",
GlobalGameId = 30014491L, GlobalAwayTeamId = 30000011L, GlobalHomeTeamId = 30000014L,
HomeTeamScore = 1L, AwayTeamScore = 4L, TotalScore = 5L,
PregameOdds = list(), LiveOdds = list(list(GameOddId = 384105L,
Sportsbook = NULL, GameId = 14491L, Created = "2019-04-14T14:26:30",
Updated = "2019-04-14T14:54:50", HomeMoneyLine = 300L,
AwayMoneyLine = -397L, HomePointSpread = 1.7, AwayPointSpread = -1.7,
HomePointSpreadPayout = -255L, AwayPointSpreadPayout = 207L,
OverUnder = 5.1, OverPayout = -187L, UnderPayout = 157L))),
list(GameId = 14492L, Season = 2019L, SeasonType = 3L, Day = "2019-04-14T00:00:00",
DateTime = "2019-04-14T19:00:00", Status = "Final", AwayTeamId = 6L,
HomeTeamId = 16L, AwayTeamName = "TB", HomeTeamName = "CBJ",
GlobalGameId = 30014492L, GlobalAwayTeamId = 30000006L,
GlobalHomeTeamId = 30000016L, HomeTeamScore = 3L, AwayTeamScore = 1L,
TotalScore = 4L, PregameOdds = list(), LiveOdds = list(
list(GameOddId = 385269L, Sportsbook = NULL, GameId = 14492L,
Created = "2019-04-14T21:16:43", Updated = "2019-04-14T21:44:55",
HomeMoneyLine = -475L, AwayMoneyLine = 327L,
HomePointSpread = -1.7, AwayPointSpread = 1.7,
HomePointSpreadPayout = 202L, AwayPointSpreadPayout = -254L,
OverUnder = 5.1, OverPayout = -174L, UnderPayout = 146L))),
list(GameId = 14493L, Season = 2019L, SeasonType = 3L, Day = "2019-04-14T00:00:00",
DateTime = "2019-04-14T19:30:00", Status = "Final", AwayTeamId = 22L,
HomeTeamId = 20L, AwayTeamName = "WPG", HomeTeamName = "STL",
GlobalGameId = 30014493L, GlobalAwayTeamId = 30000022L,
GlobalHomeTeamId = 30000020L, HomeTeamScore = 3L, AwayTeamScore = 6L,
TotalScore = 10L, PregameOdds = list(), LiveOdds = list(
list(GameOddId = 385329L, Sportsbook = NULL, GameId = 14493L,
Created = "2019-04-14T21:49:05", Updated = "2019-04-14T22:19:58",
HomeMoneyLine = NULL, AwayMoneyLine = NULL, HomePointSpread = 3.9,
AwayPointSpread = -3.9, HomePointSpreadPayout = -272L,
AwayPointSpreadPayout = 216L, OverUnder = 8.5,
OverPayout = -226L, UnderPayout = 184L))), list(
GameId = 14494L, Season = 2019L, SeasonType = 3L, Day = "2019-04-14T00:00:00",
DateTime = "2019-04-14T22:00:00", Status = "Final", AwayTeamId = 27L,
HomeTeamId = 35L, AwayTeamName = "SJ", HomeTeamName = "VEG",
GlobalGameId = 30014494L, GlobalAwayTeamId = 30000027L,
GlobalHomeTeamId = 30000035L, HomeTeamScore = 6L, AwayTeamScore = 3L,
TotalScore = 10L, PregameOdds = list(), LiveOdds = list(
list(GameOddId = 385764L, Sportsbook = NULL, GameId = 14494L,
Created = "2019-04-15T00:24:40", Updated = "2019-04-15T00:54:53",
HomeMoneyLine = NULL, AwayMoneyLine = NULL, HomePointSpread = -2.8,
AwayPointSpread = 2.8, HomePointSpreadPayout = 129L,
AwayPointSpreadPayout = -149L, OverUnder = 10.7,
OverPayout = 126L, UnderPayout = -145L))))
答案 0 :(得分:1)
由于示例数据看起来非常简单。主列表中的每个对象仅包含一个唯一列表。您可以取消列出每个对象,将其转换为data.frame,然后将它们绑定在一起。
假设您的数据名为“ jsonRespParsed”:
games<-lapply(jsonRespParsed, function(game){data.frame(t(unlist(game)))})
library(dplyr)
answer<-bind_rows(games)
answer
#GameId Season SeasonType Day DateTime Status AwayTeamId HomeTeamId AwayTeamName
#1 14491 2019 3 2019-04-14T00:00:00 2019-04-14T12:00:00 Final 11 14 NYI
#2 14492 2019 3 2019-04-14T00:00:00 2019-04-14T19:00:00 Final 6 16 TB
#3 14493 2019 3 2019-04-14T00:00:00 2019-04-14T19:30:00 Final 22 20 WPG
#4 14494 2019 3 2019-04-14T00:00:00 2019-04-14T22:00:00 Final 27 35 SJ