-1

I have a somewhat messy nested list in R of the below format which I'm having difficulty converting into a data frame.

[[1]]
[[1]]$id
[1] 101

[[1]]$resource_state
[1] 'ON'

[[1]]$athlete
[[1]]$athlete$id
[1] 10001

[[1]]$athlete$resource_state
[1] 2

[[2]]
[[2]]$id
[1] 102

[[2]]$resource_state
[1] 'OFF'

[[2]]$athlete
[[2]]$athlete$id
[1] 10001

[[2]]$athlete$resource_state
[1] 1

I've tried using sapply and lapply as well as data.table but I can't get the desired output (see below) - any ideas? Thanks

id   resource_state  athlete     athlete_resource_state
101  ON              10001       1
102  OFF             10001       2

Added dput output below to help with error (apologies, it's long!):

    Warning message:
In (function (..., deparse.level = 1)  :
  number of columns of result is not a multiple of vector length (arg 2)


    > dput(starred)
list(structure(list(id = 3096023L, resource_state = 2L, name = "Bench 1 to Bench 2", 
    activity_type = "Run", distance = 72.8203, average_grade = 3.5, 
    maximum_grade = 2.7, elevation_high = 104.8, elevation_low = 102.3, 
    start_latlng = list(53.8344181, -1.4967539), end_latlng = list(
        53.8338743, -1.4961714), start_latitude = 53.8344181, 
    start_longitude = -1.4967539, end_latitude = 53.8338743, 
    end_longitude = -1.4961714, climb_category = 0L, city = "Leeds", 
    state = "West Yorkshire", country = "United Kingdom", private = FALSE, 
    hazardous = FALSE, starred = TRUE, pr_time = 19L, athlete_pr_effort = structure(list(
        id = 13733712180, elapsed_time = 19L, distance = 68.6, 
        start_date = "2016-05-05T18:07:18Z", start_date_local = "2016-05-05T19:07:18Z", 
        is_kom = FALSE), .Names = c("id", "elapsed_time", "distance", 
    "start_date", "start_date_local", "is_kom")), starred_date = "2016-05-14T22:43:07Z"), .Names = c("id", 
"resource_state", "name", "activity_type", "distance", "average_grade", 
"maximum_grade", "elevation_high", "elevation_low", "start_latlng", 
"end_latlng", "start_latitude", "start_longitude", "end_latitude", 
"end_longitude", "climb_category", "city", "state", "country", 
"private", "hazardous", "starred", "pr_time", "athlete_pr_effort", 
"starred_date")), structure(list(id = 10490299L, resource_state = 2L, 
    name = "Regent Street - North", activity_type = "Run", distance = 408.4, 
    average_grade = 0, maximum_grade = 1.9, elevation_high = 35.1, 
    elevation_low = 32.3, start_latlng = list(53.799975, -1.533407), 
    end_latlng = list(53.80355, -1.532706), start_latitude = 53.799975, 
    start_longitude = -1.533407, end_latitude = 53.80355, end_longitude = -1.532706, 
    climb_category = 0L, city = "Leeds", state = NULL, country = "United Kingdom", 
    private = FALSE, hazardous = FALSE, starred = TRUE, pr_time = 80L, 
    athlete_pr_effort = structure(list(id = 9432540436, elapsed_time = 80L, 
        distance = 408.4, start_date = "2015-09-16T19:08:31Z", 
        start_date_local = "2015-09-16T20:08:31Z", is_kom = FALSE), .Names = c("id", 
    "elapsed_time", "distance", "start_date", "start_date_local", 
    "is_kom")), starred_date = "2016-05-14T22:40:09Z"), .Names = c("id", 
"resource_state", "name", "activity_type", "distance", "average_grade", 
"maximum_grade", "elevation_high", "elevation_low", "start_latlng", 
"end_latlng", "start_latitude", "start_longitude", "end_latitude", 
"end_longitude", "climb_category", "city", "state", "country", 
"private", "hazardous", "starred", "pr_time", "athlete_pr_effort", 
"starred_date")))

    > str(starred)
List of 2
 $ :List of 25
  ..$ id               : int 3096023
  ..$ resource_state   : int 2
  ..$ name             : chr "Bench 1 to Bench 2"
  ..$ activity_type    : chr "Run"
  ..$ distance         : num 72.8
  ..$ average_grade    : num 3.5
  ..$ maximum_grade    : num 2.7
  ..$ elevation_high   : num 105
  ..$ elevation_low    : num 102
  ..$ start_latlng     :List of 2
  .. ..$ : num 53.8
  .. ..$ : num -1.5
  ..$ end_latlng       :List of 2
  .. ..$ : num 53.8
  .. ..$ : num -1.5
  ..$ start_latitude   : num 53.8
  ..$ start_longitude  : num -1.5
  ..$ end_latitude     : num 53.8
  ..$ end_longitude    : num -1.5
  ..$ climb_category   : int 0
  ..$ city             : chr "Leeds"
  ..$ state            : chr "West Yorkshire"
  ..$ country          : chr "United Kingdom"
  ..$ private          : logi FALSE
  ..$ hazardous        : logi FALSE
  ..$ starred          : logi TRUE
  ..$ pr_time          : int 19
  ..$ athlete_pr_effort:List of 6
  .. ..$ id              : num 1.37e+10
  .. ..$ elapsed_time    : int 19
  .. ..$ distance        : num 68.6
  .. ..$ start_date      : chr "2016-05-05T18:07:18Z"
  .. ..$ start_date_local: chr "2016-05-05T19:07:18Z"
  .. ..$ is_kom          : logi FALSE
  ..$ starred_date     : chr "2016-05-14T22:43:07Z"
 $ :List of 25
  ..$ id               : int 10490299
  ..$ resource_state   : int 2
  ..$ name             : chr "Regent Street - North"
  ..$ activity_type    : chr "Run"
  ..$ distance         : num 408
  ..$ average_grade    : num 0
  ..$ maximum_grade    : num 1.9
  ..$ elevation_high   : num 35.1
  ..$ elevation_low    : num 32.3
  ..$ start_latlng     :List of 2
  .. ..$ : num 53.8
  .. ..$ : num -1.53
  ..$ end_latlng       :List of 2
  .. ..$ : num 53.8
  .. ..$ : num -1.53
  ..$ start_latitude   : num 53.8
  ..$ start_longitude  : num -1.53
  ..$ end_latitude     : num 53.8
  ..$ end_longitude    : num -1.53
  ..$ climb_category   : int 0
  ..$ city             : chr "Leeds"
  ..$ state            : NULL
  ..$ country          : chr "United Kingdom"
  ..$ private          : logi FALSE
  ..$ hazardous        : logi FALSE
  ..$ starred          : logi TRUE
  ..$ pr_time          : int 80
  ..$ athlete_pr_effort:List of 6
  .. ..$ id              : num 9.43e+09
  .. ..$ elapsed_time    : int 80
  .. ..$ distance        : num 408
  .. ..$ start_date      : chr "2015-09-16T19:08:31Z"
  .. ..$ start_date_local: chr "2015-09-16T20:08:31Z"
  .. ..$ is_kom          : logi FALSE
  ..$ starred_date     : chr "2016-05-14T22:40:09Z"
scott_001
  • 1
  • 2

1 Answers1

0

You can combine lapply with unlist:

do.call(rbind, lapply(myList, unlist))

where myList is your nested List.

Psidom
  • 209,562
  • 33
  • 339
  • 356
  • Thanks, but I'm getting an error when I try this on my full list of: Warning message: In (function (..., deparse.level = 1) : number of columns of result is not a multiple of vector length (arg 1) – scott_001 May 14 '16 at 14:29
  • Be careful that this solution does not preserve columns types but it turns everything into a character matrix (not a data.frame) – digEmAll May 14 '16 at 14:36
  • 1
    This answer will only work if your list is what you have shown in your question. If there is problem, you probably want to show more information of your list. 'dput(myList)' and 'str(myList)' will be helpful. – Psidom May 14 '16 at 14:37
  • I've added those outputs to hopefully make the error reproducible. I think I'm getting tripped up on the nested lists which aren't necessarily always the same length. Please can you take a look? – scott_001 May 14 '16 at 22:53
  • There is no `resource_state` variable in your list. Do you still want it? What variables do you need now? – Psidom May 14 '16 at 23:23
  • No, just the variables in the str(starred) list are fine. I'm trying to work with the Strava api and this is the shortest example I could find, but all of the function calls return data in the same format so I'd like to find a standardised way to turn them into data frames I can use. Thanks. – scott_001 May 15 '16 at 08:18