When using unnest_longer()
on a list column that contains tibbles the output is different than what is anticipated. I am unsure how to describe the problem so please refer to the reprex below as a supplement!
When using the legacy unnest()
, a new column is made for each column in the tibbles in the list columns. When using unnest_longer()
rather than creating new columns as described above, the unnested column becomes a tibble
column. While this looks fine when printed, it cannot be interacted with with standard dplyr functions—or if it can, it is unclear how to do so.
Is there a way to unnest the tibble as the legacy unnest()
does with unnest_longer()
? Or is there a new approach to do this within the tidyverse?
library(tidyr)
library(dplyr)
tbl_list <- list(
tibble(y = letters[1:3]),
tibble(y = letters[4:6]),
tibble(y = letters[7:9])
)
my_tbl <- tibble(id = 1:3, x = tbl_list)
(tbl_legacy_unnest <- my_tbl %>%
unnest(x))
#> # A tibble: 9 x 2
#> id y
#> <int> <chr>
#> 1 1 a
#> 2 1 b
#> 3 1 c
#> 4 2 d
#> 5 2 e
#> 6 2 f
#> 7 3 g
#> 8 3 h
#> 9 3 i
(tbl_unnest_longer <- my_tbl %>%
unnest_longer(x))
#> # A tibble: 9 x 2
#> id x$y
#> <int> <chr>
#> 1 1 a
#> 2 1 b
#> 3 1 c
#> 4 2 d
#> 5 2 e
#> 6 2 f
#> 7 3 g
#> 8 3 h
#> 9 3 i
select(tbl_legacy_unnest, y)
#> # A tibble: 9 x 1
#> y
#> <chr>
#> 1 a
#> 2 b
#> 3 c
#> 4 d
#> 5 e
#> 6 f
#> 7 g
#> 8 h
#> 9 i
select(tbl_unnest_longer, y)
#> Error: Can't subset columns that don't exist.
#> x Column `y` doesn't exist.
Created on 2020-10-06 by the reprex package (v0.3.0)