Unnesting a list of lists in a data frame column in R
Last Updated :
24 Apr, 2025
Working with data that has lists within columns is frequent when using R programming language. These lists may include various kinds of information, including other lists. But, working with these hierarchical lists can be difficult, especially if we wish to analyze or visualize the data. A list of lists in a data frame column can be de-nested to assist the data to become more manageable and simple. In this article, we will describe how to unnest a list of lists in a data frame column in R.
Concepts Related to the Topic:
Let's first go over some concepts connected to the problem before we get into how to unnest a list of lists in a data frame column. A list in R is a grouping of items that can be of various forms, including vectors, matrices, and even other lists. A data frame is a table-like structure that has rows and columns of data with a variety of data types in each column. Lists can be nested, which means they can contain additional lists or objects, within a data frame column.
In order to establish separate columns in the data frame for each element, we must unnest a list of lists within a data frame column in order to extract the data from the nested lists. This can aid in the data's simplification and ease of analysis or visualization.
Steps Needed to Unnest a Lists of Lists in a data frame:
Now let's discuss the steps needed to unnest a list of lists in a data frame column in R. We will be using the tidyr and dplyr packages to accomplish this task.
Step 1: Load the Required Packages
We need to load the tidyr and dplyr packages, which are essential for this task. We can use the following code to load the packages:
library(tidyr)
library(dplyr)
Step 2: Create the Data Frame
We will create a sample data frame to work with. Let's assume that we have a data frame with a column called "my_list," which contains a list of lists:
df <- data.frame(
id = 1:3,
my_list = list(
list(a = 1, b = "x", c = TRUE),
list(a = 2, b = "y", c = FALSE),
list(a = 3, b = "z", c = TRUE)
)
)
Step 3: Unnest the List of Lists
Now we will use the unnest() function from the tidyr package to unnest the list of lists in the "my_list" column. We will also use the mutate() function from the dplyr package to create new columns for each element in the nested lists. Here is the code:
df %>%
unnest(my_list) %>%
mutate(
a = my_list.a,
b = my_list.b,
c = my_list.c
) %>%
select(-my_list)
Explanation:
- We start by using the %>% pipe operator to pass the data frame to the unnest() function.
- The unnest() function will create a new row for each element in the nested lists.
- Next, we use the mutate() function to create new columns for each element in the nested lists.
- We specify the name of each new column and the name of the element in the nested list that we want to extract.
- Finally, we use the select() function to remove the original "my_list" column, leaving only the new columns we created.
Examples of Unnesting a List of Lists in a data frame
Example 1: Unnesting a Simple List of Lists using Tidyverse
Let's start with a simple example where we have a data frame with a list of lists containing only numeric values. Here's the code to create the sample data frame:
R
library(tidyverse)
# Create a tibble with a column containing a list of lists
tbl <- tibble(x = list(list(1,2), list(3,4), list(5,6)))
# Unnest the list of lists using unnest_longer()
unnest_list <- unnest_longer(tbl, x)
# Print the unnested list
print(unnest_list)
Output:
First, we created a data frame using the tibble() method. Then we unnest the list of lists using the unnest_longer() method. It preserves the number of columns while modifying the number of rows. Hence, the data frame created has six rows and one column.
# A tibble: 6 × 1
x
<dbl>
1 1
2 2
3 3
4 4
5 5
6 6
Example 2: Unnesting a List of Lists with Mixed Data Types
In this example, we will create a data frame with a list of lists containing both numeric and character values. Here's the code to create the sample data frame:
R
# Create a list of lists with mixed data types
list_of_lists <- list(list(1,"a"), list(2,"b"), list(3,"c"))
# Unnest the list of lists using base R
unnest_list <- unlist(list_of_lists, recursive = FALSE)
# Print the unnested list
print(unnest_list)
Output:
In this example, we used unlist() which is used to convert a list to a vector by preserving all components.
[[1]]
[1] 1
[[2]]
[1] "a"
[[3]]
[1] 2
[[4]]
[1] "b"
[[5]]
[1] 3
[[6]]
[1] "c"
Example 3: Unnesting a List of Lists with Nested Lists
Unnesting a list of lists with nested lists using the unnest() function in the tidyr package:
R
library(tidyr)
# Create a list of lists with nested lists
list_of_lists <- list(list(1, list("a", "b")), list(2, list("c", "d")), list(3, list("e", "f")))
unnested_list <- unlist(list_of_lists, recursive = FALSE)
# Print the unnested list
print(unnested_list)
Output:
In this example, we use the unlist() function to unnest the nested lists in list_of_lists and create a new list with all elements flattened. The recursive parameter is set to FALSE to only unlist the first level of the list. This will create a new data frame with six columns, one for each element in the nested lists.
Note that the output of unlist() returns a new list with all the elements flattened. If you want to convert the flattened list into a vector or a data frame, you can use as.vector() or data.frame() respectively.
[[1]]
[1] 1
[[2]]
[[2]][[1]]
[1] "a"
[[2]][[2]]
[1] "b"
[[3]]
[1] 2
[[4]]
[[4]][[1]]
[1] "c"
[[4]][[2]]
[1] "d"
[[5]]
[1] 3
[[6]]
[[6]][[1]]
[1] "e"
[[6]][[2]]
[1] "f"
Conclusion
Unnesting a list of lists in a data frame column can be done easily using the unnest() function from the tidyr package in R. The process involves two steps: first, we unnest the list column to create multiple rows, one for each element in the nested lists, and then we extract each element and create new columns for them. With the help of examples, we have seen how to unnest simple and complex lists of lists with different data types and structures.
Similar Reads
Shift a column of lists in data.table by group in R
In this article, we will discuss how to shift a column of lists in data.table by a group in R Programming Language. The data table subsetting can be performed and the new column can be created and its values are assigned using the shift method in R. The type can be specified as either "lead" or "lag
2 min read
Unnest (Explode) Multiple List Columns In A Pandas Dataframe
An open-source manipulation tool that is used for handling data is known as Pandas. Have you ever encountered a dataset that has columns with data as a list? In such cases, there is a necessity to split that column into various columns, as Pandas cannot handle such data. In this article, we will dis
6 min read
How to Convert a List to a Dataframe in R
We have a list of values and if we want to Convert a List to a Dataframe within it, we can use a as.data.frame. it Convert a List to a Dataframe for each value. A DataFrame is a two-dimensional tabular data structure that can store different types of data. Various functions and packages, such as dat
4 min read
Change column name of a given DataFrame in R
A data frame is a tabular structure with fixed dimensions, of each rows as well as columns. It is a two-dimensional array like object with numerical, character based or factor-type data. Each element belonging to the data frame is indexed by a unique combination of the row and column number respecti
6 min read
Get a List of a Specific Column of a Pandas DataFrame
In data analysis, extracting specific columns from a DataFrame and converting them into Python lists is a common requirement. Pandas provides multiple ways to achieve this efficiently. This article explores various methods to extract a specific column from a Pandas DataFrame and convert it into a li
3 min read
Convert an Excel column into a list of vectors in R
In this article, we will be discussing the different approaches to convert the Excel columns to vector in the R Programming language. File in use: Method 1: Using $-Operator with the column name In this method, we will be simply using the $-operator with the column name and the name of the data read
2 min read
How to add multiple columns to a data.frame in R?
In R Language adding multiple columns to a data.frame can be done in several ways. Below, we will explore different methods to accomplish this, using some practical examples. We will use the base R approach, as well as the dplyr package from the tidyverse collection of packages.Understanding Data Fr
4 min read
Change more than one column name of a given DataFrame in R
A data frame is a tabular structure with fixed dimensions, of each row as well as columns. It is a two-dimensional array-like object with numerical, character-based, or factor-type data. Each element belonging to the data frame is indexed by a unique combination of the row and column number respecti
4 min read
How to Convert a List to a DataFrame Row in Python?
In this article, we will discuss how to convert a list to a dataframe row in Python. Method 1: Using T function This is known as the Transpose function, this will convert the list into a row. Here each value is stored in one column. Syntax: pandas.DataFrame(list).T Example: Python3 # import pandas m
3 min read
Plot columns from list of dataframes in R
In this article, we will discuss how to plot columns from a list of dataframes in R programming language. Note: We are taking a line plot for implementation same can be applied to any other plot. The ggplot() method is supplemented by different geometrical shapes to indicate the type of data plotti
2 min read