group_nest(.tbl, ..., .key = "data", keep = FALSE)
.tbl | A tbl |
---|---|
... | Grouping specification, forwarded to |
.key | the name of the list column |
keep | Should the grouping columns be kept in the list column. |
A tbl with one row per unique combination of the grouping variables. The first columns are the grouping variables, followed by a list column of tibbles with matching rows of the remaining columns.
The primary use case for group_nest()
is with already grouped data frames,
typically a result of group_by()
. In this case group_nest()
only uses
the first argument, the grouped tibble, and warns when ...
is used.
When used on ungrouped data frames, group_nest()
forwards the ...
to
group_by()
before nesting, therefore the ...
are subject to the data mask.
Other grouping functions:
group_by()
,
group_map()
,
group_split()
,
group_trim()
#> # A tibble: 3 x 2 #> Species data #> <fct> <list<tibble[,4]>> #> 1 setosa [50 × 4] #> 2 versicolor [50 × 4] #> 3 virginica [50 × 4]# this can be useful if the grouped data has been altered before nesting iris %>% group_by(Species) %>% filter(Sepal.Length > mean(Sepal.Length)) %>% group_nest()#> # A tibble: 3 x 2 #> Species data #> <fct> <list<tibble[,4]>> #> 1 setosa [22 × 4] #> 2 versicolor [24 × 4] #> 3 virginica [22 × 4]#----- use case 2: using group_nest() on a ungrouped data frame with # a grouping specification that uses the data mask starwars %>% group_nest(species, homeworld)#> # A tibble: 58 x 3 #> species homeworld data #> <chr> <chr> <list<tibble[,12]>> #> 1 Aleena Aleen Minor [1 × 12] #> 2 Besalisk Ojom [1 × 12] #> 3 Cerean Cerea [1 × 12] #> 4 Chagrian Champala [1 × 12] #> 5 Clawdite Zolan [1 × 12] #> 6 Droid Naboo [1 × 12] #> 7 Droid Tatooine [2 × 12] #> 8 Droid NA [3 × 12] #> 9 Dug Malastare [1 × 12] #> 10 Ewok Endor [1 × 12] #> # … with 48 more rows