Convert Nested Lists to Data Frame or Matrix in R (2 Examples)

 

On this page, I’ll illustrate how to store a list of lists in a data frame or matrix in the R programming language.

The tutorial will consist of these topics:

Let’s jump right to the examples…

 

Creation of Example Data

The first step is to define some data that we can use in the examples below:

my_nested_list <- list(l1 = list(1:5,                         # Create nested list
                                 letters[3:1],
                                 "x"),
                       l2 = list(10:15,
                                 letters[13:11],
                                 "y"),
                       l3 = list("This",
                                 "is another",
                                 "list"))
my_nested_list                                                # Print nested list
# $l1
# $l1[[1]]
# [1] 1 2 3 4 5
# 
# $l1[[2]]
# [1] "c" "b" "a"
# 
# $l1[[3]]
# [1] "x"
# 
# 
# $l2
# $l2[[1]]
# [1] 10 11 12 13 14 15
# 
# $l2[[2]]
# [1] "m" "l" "k"
# 
# $l2[[3]]
# [1] "y"
# 
# 
# $l3
# $l3[[1]]
# [1] "This"
# 
# $l3[[2]]
# [1] "is another"
# 
# $l3[[3]]
# [1] "list"
# 
#

The previous output of the RStudio console shows that our example data is a nested list containing three sub-lists.

 

Example 1: Convert List of Lists to Data Frame by Column

This example explains how to convert our nested list to a data frame where each column of the data frame contains one of the sub-lists.

For this, we can use the do.call and cbind functions as shown below.

In the following R code we also use the as.data.frame function to convert our lists to a data frame instead of a matrix object.

Have a look at the following R code and its output:

my_list_data_cbind <- as.data.frame(do.call(cbind,            # Convert nested list to data frame by column
                                            my_nested_list))
 my_list_data_cbind # Print nested list in data frame
#              l1                     l2         l3
# 1 1, 2, 3, 4, 5 10, 11, 12, 13, 14, 15       This
# 2       c, b, a                m, l, k is another
# 3             x                      y       list

As you can see, we have created a data frame where each column contains a list.

We can now extract single lists from this data frame using the $ operator:

my_list_data_cbind$l1                                         # Print one element of nested list in data frame
# [[1]]
# [1] 1 2 3 4 5
# 
# [[2]]
# [1] "c" "b" "a"
# 
# [[3]]
# [1] "x"
#

The previous R code has printed the first sub-list of our nested list (or the first variable of our new data frame respectively) to the RStudio console.

 

Example 2: Convert List of Lists to Matrix by Row

Example 2 shows how to bind the sub-lists of a nested list as rows in a matrix object.

For this, we can use the do.call and rbind functions as shown below:

my_list_data_rbind <- do.call(rbind,                          # Convert nested list to matrix by row
                              my_nested_list)
my_list_data_rbind # Print nested list in matrix
#    [,1]      [,2]         [,3]  
# l1 Integer,5 Character,3  "x"   
# l2 Integer,6 Character,3  "y"   
# l3 "This"    "is another" "list"

We can now extract certain lists by subsetting our matrix:

my_list_data_rbind["l1", ]                                    # Print one element of nested list in matrix
# [[1]]
# [1] 1 2 3 4 5
# 
# [[2]]
# [1] "c" "b" "a"
# 
# [[3]]
# [1] "x"
#

 

Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which explains how to flatten a list of lists based on the examples of this post. You can find the video tutorial below.

 

 

Also, you might read the other RStudio posts of my website. I have published several related tutorials about related topics such as data conversion, factors, character strings, matrices, and nested lists.

 

In this tutorial you have learned how to convert and cast nested lists as data frames and matrices in the R programming language. If you have further questions, let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


4 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top