Nest repeated values in a list-variable.

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)

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.


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.

  • nest
iris %>% nest(-Species)
chickwts %>% nest(weight)

if (require("gapminder")) {
  gapminder %>%
    group_by(country, continent) %>%

  gapminder %>%
    nest(-country, -continent)
# }

Community examples at Jun 2, 2019 tidyr v0.8.3

# Simple example of nest function usage **#make simple dataframe of 2 columns** df <- data.frame(name=c("a","b","b","c","c","c"), count=c(1,2,3,1,2,3)) **#nest second column within dataframe** df %>% nest(count) at Aug 20, 2018 tidyr v0.8.1

## This example is to demonstrate the nest function on the iris dataset ``` # Load tidyverse library library(tidyverse) # Load native iris dataset df <- iris # Check names of columns to find out which column you want to subset on names(df) # Since we want to "group by" the species type we can either group all other variables we want in a list, or just exclude the species column if we want the remaining variables new_df <- df %>% nest(-Species) # Check that the new dataframe is what we expected new_df ```