Reputation: 1969
I have a tibble with a list-column of lists. In the case below, the list is the unique models for each of the carb-gear combinations.
my_df <- mtcars %>%
rownames_to_column('model') %>%
as_tibble() %>%
select(model, gear, carb) %>%
group_by(carb, gear) %>%
summarise(models = list(unique(model)))
# A tibble: 11 × 3
# Groups: carb [6]
carb gear models
<dbl> <dbl> <list>
1 1 3 <chr [3]>
2 1 4 <chr [4]>
3 2 3 <chr [4]>
4 2 4 <chr [4]>
5 2 5 <chr [2]>
6 3 3 <chr [3]>
7 4 3 <chr [5]>
8 4 4 <chr [4]>
9 4 5 <chr [1]>
10 6 5 <chr [1]>
11 8 5 <chr [1]>
Now say I want to roll-up the unique models to be just the unique models for each gear. I would expect to do something like the below, but it doesn't seem to honoring the groups.
my_df %>%
group_by(gear) %>%
summarise(models = map(models, c))
I expect the result to look like this:
# A tibble: 3 × 2
# Groups: gear [3]
gear models
<dbl> <list>
1 3 <chr [15]>
2 4 <chr [12]>
3 5 <chr [5]>
Upvotes: 1
Views: 308
Reputation: 388817
You can unlist
the models
to bring them in one vector and stire it in list
again.
library(dplyr)
my_df %>% group_by(gear) %>% summarise(models = list(unlist(models)))
# gear models
# <dbl> <list>
#1 3 <chr [15]>
#2 4 <chr [12]>
#3 5 <chr [5]>
Upvotes: 1