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.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
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.
- Select First Element of Nested List
- Index Element of List in R
- Select Random Element from List
- Get Second Sub Entry of Every List Element
- Select Multiple Elements from List
- Introduction to R
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.
Statistics Globe Newsletter
4 Comments. Leave new
My main problem with importing nested lists from JSON files is, that the nested lists are also not of the same size. I have the same number of variables for each entry, but some entries items have different data entry list lengths.
What are the best ways to go about it then?
Hey John,
Unfortunately, I’m not an expert on JSON files. However, I have recently created a Facebook discussion group where people can ask questions about R programming and statistics. Could you post your question there? This way, others can contribute/read as well: https://www.facebook.com/groups/statisticsglobe
Regards,
Joachim
Often saw this site as an aspirator of stackoverflow content, but this really helped, so thanks a lot
Hey Matt,
Thank you very much for the kind words, glad you liked our tutorial!
Regards,
Matthias