Open In App

Unnesting a list of lists in a data frame column in R

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads