R nest() Function from tidyr

The nest() function from the tidyr package in R is used to create a nested data frame. It takes multiple columns of a data frame and collapses them into a single list-column, where each element of this list-column is a tibble/data frame containing the data for each group.

This method is often used after the group_by() function to encapsulate group data but can also be used directly without prior grouping.

Syntax

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

Parameters

Name Description
data It is the data frame or tibble to be nested.
The columns to nest. You can use tidy-select semantics to specify these columns.
.key The name of the new column that will contain the nested data frames.

Return value

It returns a nested data frame.

Example 1: Basic usage

Understanding of tidyr nest() function in R

library(tidyr)
library(dplyr)

df <- data.frame(
  name = c("Krunal", "Ankit", "Rushabh", "Niva", "Hemang", "Vidisha"),
  score = c(85, 90, 78, 95, 80, 92),
  subject = c("Math", "Math", "History", "History", "Math", "History"),
  grade = c("10th", "11th", "11th", "10th", "11th", "10th")
)

df %>%
  group_by(subject) %>%
  nest()

Output

Output of tidyr nest() Function

Example 2: Specifying columns to nest

Let’s nest the “subject” and “score” columns of the data frame.

Specifying columns to nest

We will do the nesting without prior grouping.

library(tidyr)

df <- data.frame(
  name = c("Krunal", "Ankit", "Rushabh", "Niva", "Hemang", "Vidisha"),
  score = c(85, 90, 78, 95, 80, 92),
  subject = c("Math", "Math", "History", "History", "Math", "History"),
  grade = c("10th", "11th", "11th", "10th", "11th", "10th")
)

df %>%
 nest(data = c(subject, score))

Output

Output of Specifying columns to nest

The above code explicitly nested only “subject” and “score”, naming the list-column as data.

The nest() function is helpful for creating hierarchical or multi-level data structures, making it easier to apply functions to each subset of data separately.

Leave a Comment