There are many possible ways one could choose to nest columns inside a data frame. nest() creates a list of data frames containing all the nested variables: this seems to be the most useful form in practice.

nest(data, ..., .key = data)

Arguments

data

A data frame.

...

Specification of columns to nest. Use bare variable names. Select all variables between x and z with x:z, exclude y with -y. For more options, see the select documentation.

.key

The name of the new column.

See also

unnest for the inverse operation. nest_ for a version that uses regular evaluation and is suitable for programming with.

Examples

library(dplyr) iris %>% nest(-Species)
#> # A tibble: 3 × 2 #> Species data #> <fctr> <list> #> 1 setosa <tibble [50 × 4]> #> 2 versicolor <tibble [50 × 4]> #> 3 virginica <tibble [50 × 4]>
chickwts %>% nest(weight)
#> # A tibble: 6 × 2 #> feed data #> <fctr> <list> #> 1 horsebean <tibble [10 × 1]> #> 2 linseed <tibble [12 × 1]> #> 3 soybean <tibble [14 × 1]> #> 4 sunflower <tibble [12 × 1]> #> 5 meatmeal <tibble [11 × 1]> #> 6 casein <tibble [12 × 1]>
if (require("gapminder")) { gapminder %>% group_by(country, continent) %>% nest() gapminder %>% nest(-country, -continent) }
#> Loading required package: gapminder
#> Warning: there is no package called ‘gapminder’