Extracting data from a list of lists into its own `data.frame` with `purrr`

前端 未结 2 1271
说谎
说谎 2021-01-01 08:03

Representative sample data (list of lists):

l <- list(structure(list(a = -1.54676469632688, b = \"s\", c = \"T\", 
d = structure(list(id = 5L, label = \"U         


        
2条回答
  •  不知归路
    2021-01-01 08:07

    You can do this in three steps, first pulling out d, then binding the rows within each element of d, and then binding everything into a single object.

    I use bind_rows from dplyr for the within-list row binding. map_df does the final row binding.

    library(purrr)
    library(dplyr)
    
    l %>%
        map("d") %>%
        map_df(bind_rows)
    

    This is also equivalent:

    map_df(l, ~bind_rows(.x[["d"]] ) )
    

    The result looks like:

    # A tibble: 12 x 5
          id          label                 link      score externalId
                                             
     1     5           Utah          Asia/Anadyr -0.2110459         NA
     2     8 South Carolina       Pacific/Wallis  0.5265409  -6.743544
     3     9       Nebraska America/Scoresbysund  0.2508955  16.425747
     4     8        Georgia         America/Nome  0.5264941   7.915836
     5     2     Washington     America/Shiprock -0.5551864  15.068666
     6     6   North Dakota            Universal  1.0316830         NA
     7     1  New Hampshire      America/Cordoba  1.2158206   9.727642
     8     1         Alaska        Asia/Istanbul -0.2318326         NA
     9     4   Pennsylvania Africa/Dar_es_Salaam  0.5902453         NA
    10     3       Delaware       Asia/Samarkand  0.6955771  15.236482
    11     4   North Dakota      America/Tortola  1.0306027  -7.216669
    12     9       Nebraska      America/Ojinaga -1.1139800  -8.451451
    

提交回复
热议问题