Unnesting a list of lists in a data frame column

眉间皱痕 提交于 2019-11-28 22:55:01

问题


To unnest a data frame I can use:

df <- data_frame(
    x = 1,
    y = list(a = 1, b = 2)
)

tidyr::unnest(df)

But how can I unnest a list inside of a list inside of a data frame column?

df <- data_frame(
    x = 1,
    y = list(list(a = 1, b = 2))
)
tidyr::unnest(df)

Error:

Each column must either be a list of vectors or a list of data frames [y]


回答1:


With purrr, which is nice for lists,

library(purrr)

df %>% dmap(unlist)
## # A tibble: 2 x 2
##       x     y
##   <dbl> <dbl>
## 1     1     1
## 2     1     2

which is more or less equivalent to

as.data.frame(lapply(df, unlist))
##   x y
## a 1 1
## b 1 2

Update:

dmap has been deprecated and moved to purrrlyr, the home of interesting but ill-fated functions that will now shout lots of deprecation warnings at you. You could translate the base R idiom to tidyverse:

df %>% map(unlist) %>% as_data_frame()

which will work fine for this case, but not for more than one row (a problem all these approaches face). A more robust solution might be

library(tidyverse)

df %>% bind_rows(df) %>%    # make larger sample data
    mutate_if(is.list, simplify_all) %>%    # flatten each list element internally 
    unnest()    # expand
#> # A tibble: 4 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1     1     1
#> 2     1     2
#> 3     1     1
#> 4     1     2



回答2:


This can be done in a simple step using unnest_longer() since tidyr 1.0.0 :

df <- tibble::tibble(
  x = 1,
  y = list(list(a = 1, b = 2))
)

library(tidyr)
unnest_longer(df,y,indices_include = FALSE)
#> # A tibble: 2 x 2
#>       x     y
#>   <dbl> <dbl>
#> 1     1     1
#> 2     1     2

Created on 2019-09-14 by the reprex package (v0.3.0)



来源:https://stackoverflow.com/questions/38860380/unnesting-a-list-of-lists-in-a-data-frame-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!