【发布时间】:2019-04-17 08:47:16
【问题描述】:
我有一个相当复杂的多级列表:
my_list <- list(list(id = 36L, name = "Marathonbet", odds = list(data = list(
list(label = "1", value = "1.25", dp3 = "1.250", american = "-400",
winning = TRUE, handicap = NULL, total = NULL, bookmaker_event_id = "6938899",
last_update = list(date = "2018-08-12 13:12:23.000000",
timezone_type = 3L, timezone = "UTC")), list(label = "2",
value = "13.75", dp3 = "13.750", american = "1275", winning = FALSE,
handicap = NULL, total = NULL, bookmaker_event_id = "6938899",
last_update = list(date = "2018-08-12 13:12:23.000000",
timezone_type = 3L, timezone = "UTC")), list(label = "X",
value = "7.00", dp3 = "7.000", american = "600", winning = FALSE,
handicap = NULL, total = NULL, bookmaker_event_id = "6938899",
last_update = list(date = "2018-08-12 13:12:23.000000",
timezone_type = 3L, timezone = "UTC"))))), list(id = 7L,
name = "888Sport", odds = list(data = list(list(label = "1",
value = "1.23", dp3 = "1.230", american = "-435", winning = TRUE,
handicap = NULL, total = NULL, bookmaker_event_id = "1004746417",
last_update = list(date = "2018-08-12 13:12:23.000000",
timezone_type = 3L, timezone = "UTC")), list(label = "2",
value = "12.50", dp3 = "12.500", american = "1150", winning = FALSE,
handicap = NULL, total = NULL, bookmaker_event_id = "1004746417",
last_update = list(date = "2018-08-12 13:12:23.000000",
timezone_type = 3L, timezone = "UTC")), list(label = "X",
value = "6.50", dp3 = "6.500", american = "550", winning = FALSE,
handicap = NULL, total = NULL, bookmaker_event_id = "1004746417",
last_update = list(date = "2018-08-12 13:12:23.000000",
timezone_type = 3L, timezone = "UTC"))))), list(id = 9L,
name = "BetFred", odds = list(data = list(list(label = "1",
value = "1.30", dp3 = NULL, american = NULL, winning = TRUE,
handicap = NULL, total = NULL, bookmaker_event_id = "1085457020",
last_update = list(date = "2018-07-26 08:30:19.000000",
timezone_type = 3L, timezone = "UTC")), list(label = "2",
value = "9.00", dp3 = NULL, american = NULL, winning = FALSE,
handicap = NULL, total = NULL, bookmaker_event_id = "1085457020",
last_update = list(date = "2018-07-26 08:30:19.000000",
timezone_type = 3L, timezone = "UTC")), list(label = "X",
value = "5.50", dp3 = NULL, american = NULL, winning = FALSE,
handicap = NULL, total = NULL, bookmaker_event_id = "1085457020",
last_update = list(date = "2018-07-26 08:30:19.000000",
timezone_type = 3L, timezone = "UTC"))))))
我可以使用map 和map_depth 的组合来消除嵌套级别,但是我正在努力将这些级别绑定到数据框中并保留所有数据。例如 - 在级别 my_list[[1]][["odds"]][["data"]] 有三个子列表。将该级别转换为 df 时,我只在应该有 3 行数据时得到一行数据。
我想做的是将整个列表转换为一个数据框,其中跨子列表的公共元素,例如:
my_list[[1]][["odds"]][["data"]][[1]][["bookmaker_event_id"]] &
my_list[[2]][["odds"]][["data"]][[1]][["bookmaker_event_id"]]
出现在结果 df 的同一列中。
这似乎是一件很容易实现的事情,但我最终要么丢失了数据行,要么Error: Argument 1 must have names。此测试列表生成的数据框应该有 9 行和大约 13 列。
我想使用map 系列函数并避免任何循环。
【问题讨论】:
-
来自 JSON - 我将
httr与外部 API 一起使用