Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

group_nest

Nest a tibble using a grouping specification


Description

[Experimental]

Nest a tibble using a grouping specification

Usage

group_nest(.tbl, ..., .key = "data", keep = FALSE)

Arguments

.tbl

A tbl

...

Grouping specification, forwarded to group_by()

.key

the name of the list column

keep

Should the grouping columns be kept in the list column.

Value

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.

Grouped data frames

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.

Ungrouped data frames

When used on ungrouped data frames, group_nest() forwards the ... to group_by() before nesting, therefore the ... are subject to the data mask.

See Also

Other grouping functions: group_by(), group_map(), group_split(), group_trim()

Examples

#----- use case 1: a grouped data frame
iris %>%
  group_by(Species) %>%
  group_nest()

# 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()

#----- 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)

dplyr

A Grammar of Data Manipulation

v1.0.6
MIT + file LICENSE
Authors
Hadley Wickham [aut, cre] (<https://orcid.org/0000-0003-4757-117X>), Romain François [aut] (<https://orcid.org/0000-0002-2444-4226>), Lionel Henry [aut], Kirill Müller [aut] (<https://orcid.org/0000-0002-1416-3412>), RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.