将一列列表取消嵌套到 tidyr 中的多列

Unnest one column list to many columns in tidyr

例如,我有这样一个整洁的数据框:

df <- tibble(id=1:2,
         ctn=list(list(a="x",b=1),
                  list(a="y",b=2)))
# A tibble: 2 x 2
     id        ctn
  <int>     <list>
1     1 <list [2]>
2     2 <list [2]>

如何取消向右嵌套 ctn 列,以便数据框如下所示:

# A tibble: 2 x 3
     id     a     b
  <int> <chr> <dbl>
1     1     x     1
2     2     y     2

一个选项是

library(data.table)
setDT(df)[, unlist(ctn, recursive = FALSE), id]
#   id a b
#1:  1 x 1
#2:  2 y 2

tidyr

library(tidyverse)
df$ctn %>%
     setNames(., df$id) %>%
     bind_rows(., .id = 'id')
# A tibble: 2 x 3
#   id     a     b
#  <chr> <chr> <dbl>
#1     1     x     1
#2     2     y     2

dplyrpurrr

df %>% 
  mutate(ctn = map(ctn, as_tibble)) %>%
  unnest()
# A tibble: 2 x 3
     id     a     b
  <int> <chr> <dbl>
1     1     x     1
2     2     y     2

我们现在可以(dplyr 1.0.2 及更高版本)以一种简洁的方式使用 rowwise():

df %>% rowwise() %>% mutate(as_tibble(ctn))
# A tibble: 2 x 4
# Rowwise: 
     id ctn              a         b
  <int> <list>           <chr> <dbl>
1     1 <named list [2]> x         1
2     2 <named list [2]> y         2

并且坚持 purrr 我们还可以:

df %>% mutate(map_dfr(ctn, as_tibble))
# A tibble: 2 x 4
     id ctn              a         b
  <int> <list>           <chr> <dbl>
1     1 <named list [2]> x         1
2     2 <named list [2]> y         2