I have a reasonably complicated multi-level list:
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"))))))
I can use a combination of map
and map_depth
to eliminate levels of nesting, but I'm struggling then to bind those levels into a data frame and preserve all the data. For example - at level my_list[[1]][["odds"]][["data"]]
there are three sub lists. When converting that level to a df I only end up with one row of data when there should be 3.
What I would like to do is convert this entire list to a data frame, where the common elements across sublists such as:
my_list[[1]][["odds"]][["data"]][[1]][["bookmaker_event_id"]]
&
my_list[[2]][["odds"]][["data"]][[1]][["bookmaker_event_id"]]
appear in the same column in the resulting df.
It seems like a easy thing to achieve, but I either end up with missing rows of data or Error: Argument 1 must have names
. The resulting data frame from this test list should have 9 rows and around 13 columns.
I'd like to use the map
family of functions and avoid any loops please.