I'm sorry, I'm new to tidyr
package and I would be grateful if someone could explain me this behaviour. So, I've noticed that unnest_longer()
is not able to unnest single row nested data frames as unnest()
do. Is it expected to behave like this?
Here is a small example:
library(tidyr)
library(magrittr)
nested_a <- tibble(A = 'a', B = 'b', C = 'c')
test_a <- tibble( col = list(nested_a) )
test_a %>% tidyr::unnest_longer(col)
#> Error: Assigned data `map2(...)` must be compatible with existing data.
#> x Existing data has 1 row.
#> x Assigned data has 3 rows.
#> i Row updates require a list value. Do you need `list()` or `as.list()`?
#> Run `rlang::last_error()` to see where the error occurred.
It works with unnest()
:
test_a %>% tidyr::unnest(col)
#> # A tibble: 1 x 3
#> A B C
#> <chr> <chr> <chr>
#> 1 a b c
Also with nested data frames with 2+ rows:
nested_b <- tibble( A = c('a', 'a'), B = c('b', 'b'), C = c('c', 'c') )
test_b <- tibble( col = list(nested_b))
test_b %<>% tidyr::unnest_longer(col)
test_b %>% unpack(col)
#> # A tibble: 2 x 3
#> A B C
#> <chr> <chr> <chr>
#> 1 a b c
#> 2 a b c