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

nest_by

Nest by one or more variables


Description

[Experimental]

nest_by() is closely related to group_by(). However, instead of storing the group structure in the metadata, it is made explicit in the data, giving each group key a single row along with a list-column of data frames that contain all the other data.

nest_by() returns a rowwise data frame, which makes operations on the grouped data particularly elegant. See vignette("rowwise") for more details.

Usage

nest_by(.data, ..., .key = "data", .keep = FALSE)

Arguments

.data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.

...

In group_by(), variables or computations to group by. In ungroup(), variables to remove from the grouping.

.key

Name of the list column

.keep

Should the grouping columns be kept in the list column.

Details

Note that df %>% nest_by(x, y) is roughly equivalent to

df %>%
  group_by(x, y) %>%
  summarise(data = list(cur_data())) %>%
  rowwise()

If you want to unnest a nested data frame, you can either use tidyr::unnest() or take advantage of summarise()s multi-row behaviour:

nested %>%
  summarise(data)

Value

A rowwise data frame. The output has the following properties:

  • The rows come from the underlying group_keys().

  • The columns are the grouping keys plus one list-column of data frames.

  • Data frame attributes are not preserved, because nest_by() fundamentally creates a new data frame.

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.

Methods

This function is a generic, which means that packages can provide implementations (methods) for other classes. See the documentation of individual methods for extra arguments and differences in behaviour.

The following methods are currently available in loaded packages: no methods found.

Examples

# After nesting, you get one row per group
iris %>% nest_by(Species)
starwars %>% nest_by(species)

# The output is grouped by row, which makes modelling particularly easy
models <- mtcars %>%
  nest_by(cyl) %>%
  mutate(model = list(lm(mpg ~ wt, data = data)))
models

models %>% summarise(rsq = summary(model)$r.squared)
# This is particularly elegant with the broom functions
if (requireNamespace("broom", quietly = TRUE)) {
  models %>% summarise(broom::glance(model))
  models %>% summarise(broom::tidy(model))
}

# Note that you can also summarise to unnest the data
models %>% summarise(data)

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.