I have a tibble I would like to nest() and then unnest_wider(), while also maintaining a copy of the nested data in tibble format. I know this sounds not very elegant, but this is the best solution for now for my use case. however, when I use the unnest_wider() function, the name_repair creates ugly ...1, ...2, etc. names. How can I name the items in the list (they are of different lengths) using some purrr function (https://community.rstudio.com/t/how-to-handle-lack-of-names-with-unnest-wider/40496)? so that when I unnest_wider() the columns have nicer names.
a small example of what I am looking for:
library(tidyverse)
mpg %>%
select(manufacturer, model, cty ) %>%
group_by(manufacturer, model) %>%
nest() %>%
mutate(vars_in_tibble = data) %>%
ungroup() %>%
unnest_wider(data) %>%
unnest_wider(cty)
gives
# A tibble: 38 x 14
manufacturer model ...1 ...2 ...3 ...4 ...5 ...6 ...7 ...8 ...9 ...10 ...11 vars_in_tibble
<chr> <chr> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <list>
1 audi a4 18 21 20 21 16 18 18 NA NA NA NA <tibble[,1] [7 x 1]>
2 audi a4 quattro 18 16 20 19 15 17 17 15 NA NA NA <tibble[,1] [8 x 1]>
3 audi a6 quattro 15 17 16 NA NA NA NA NA NA NA NA <tibble[,1] [3 x 1]>
4 chevrolet c1500 suburban 2wd 14 11 14 13 12 NA NA NA NA NA NA <tibble[,1] [5 x 1]>
5 chevrolet corvette 16 15 16 15 15 NA NA NA NA NA NA <tibble[,1] [5 x 1]>
6 chevrolet k1500 tahoe 4wd 14 11 11 14 NA NA NA NA NA NA NA <tibble[,1] [4 x 1]>
7 chevrolet malibu 19 22 18 18 17 NA NA NA NA NA NA <tibble[,1] [5 x 1]>
8 dodge caravan 2wd 18 17 16 16 17 17 11 15 15 16 16 <tibble[,1] [11 x 1]>
9 dodge dakota pickup 4wd 15 14 13 14 14 14 9 11 11 NA NA <tibble[,1] [9 x 1]>
10 dodge durango 4wd 13 13 9 13 11 13 11 NA NA NA NA <tibble[,1] [7 x 1]>
but I want
# A tibble: 38 x 14
manufacturer model car_1 car_2 car_3 car_4 car_5 car_6 car_7 car_8 car_9 car_10 car_11 vars_in_tibble
<chr> <chr> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <list>
1 audi a4 18 21 20 21 16 18 18 NA NA NA NA <tibble[,1] [7 x 1]>
2 audi a4 quattro 18 16 20 19 15 17 17 15 NA NA NA <tibble[,1] [8 x 1]>
3 audi a6 quattro 15 17 16 NA NA NA NA NA NA NA NA <tibble[,1] [3 x 1]>
4 chevrolet c1500 suburban 2wd 14 11 14 13 12 NA NA NA NA NA NA <tibble[,1] [5 x 1]>
5 chevrolet corvette 16 15 16 15 15 NA NA NA NA NA NA <tibble[,1] [5 x 1]>
6 chevrolet k1500 tahoe 4wd 14 11 11 14 NA NA NA NA NA NA NA <tibble[,1] [4 x 1]>
7 chevrolet malibu 19 22 18 18 17 NA NA NA NA NA NA <tibble[,1] [5 x 1]>
8 dodge caravan 2wd 18 17 16 16 17 17 11 15 15 16 16 <tibble[,1] [11 x 1]>
9 dodge dakota pickup 4wd 15 14 13 14 14 14 9 11 11 NA NA <tibble[,1] [9 x 1]>
10 dodge durango 4wd 13 13 9 13 11 13 11 NA NA NA NA <tibble[,1] [7 x 1]>