Reputation: 1669
tidyr::nest()
creates a list-column of data frames/tibbles:
library(tidyverse)
iris %>% nest(data = c(-Species))
#> # A tibble: 3 × 2
#> Species data
#> <fct> <list>
#> 1 setosa <tibble [50 × 4]>
#> 2 versicolor <tibble [50 × 4]>
#> 3 virginica <tibble [50 × 4]>
This is of course useful in lots of cases. In other cases, what I want is to do the same sort of implicit summarizing operation, but instead of creating a single list-column of tibbles, I want to turn each selected column into a list-column of vectors.
What I'm looking for can be accomplished by following nest()
with unnest_wider()
:
iris %>% nest(data = c(-Species)) %>%
unnest_wider(data)
#> # A tibble: 3 × 5
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#> <fct> <list<dbl>> <list<dbl>> <list<dbl>> <list<dbl>>
#> 1 setosa [50] [50] [50] [50]
#> 2 versicolor [50] [50] [50] [50]
#> 3 virginica [50] [50] [50] [50]
It can also be accomplished by a more explicit summarizing operation:
iris %>%
group_by(Species) %>%
summarize(across(everything(), vctrs::list_of))
#> # A tibble: 3 × 5
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#> <fct> <list<dbl>> <list<dbl>> <list<dbl>> <list<dbl>>
#> 1 setosa [50] [50] [50] [50]
#> 2 versicolor [50] [50] [50] [50]
#> 3 virginica [50] [50] [50] [50]
But both of these seem a little unwieldly, and I'm wondering if there is a more parsimonious or more standard way of doing this in tidyr
or related tidyverse packages.
Upvotes: 5
Views: 125
Reputation: 5137
This saves about 30 characters typed :
iris |>
group_by(Species) |>
summarise_all(list)
Upvotes: 0