Create Nested List in R (2 Examples)

 

In this post, I’ll show how to build a list of lists in the R programming language.

The article contains these contents:

Here’s the step-by-step process!

 

Introducing Exemplifying Data

Have a look at the three example lists below:

list_1 <- list(12:20,                             # Create first example list
               letters[16:11],
               "yyyy")
list_1                                            # Print first example list
# [[1]]
# [1] 12 13 14 15 16 17 18 19 20
# 
# [[2]]
# [1] "p" "o" "n" "m" "l" "k"
# 
# [[3]]
# [1] "yyyy"
#
list_2 <- list(4:8,                               # Create second example list
               letters[7:1],
               "xxx")
list_2                                            # Print second example list
# [[1]]
# [1] 4 5 6 7 8
# 
# [[2]]
# [1] "g" "f" "e" "d" "c" "b" "a"
# 
# [[3]]
# [1] "xxx"
#
list_3 <- list("Another",                         # Create third example list
               "list",
               "in R")
list_3                                            # Print third example list
# [[1]]
# [1] "Another"
# 
# [[2]]
# [1] "list"
# 
# [[3]]
# [1] "in R"
#

As you can see based on the previously shown output of the RStudio console, we have created three list objects in R. Let’s combine these data in a nested list!

 

Example 1: Create List of Lists Using list() Function

The following R programming code shows how to merge multiple list objects in a nested list using the list() function in R.

For this, we simply have to specify the names of our lists separated by a comma within the list function:

my_nested_list1 <- list(list_1,                   # Create nested list using list()
                        list_2,
                        list_3)
my_nested_list1                                   # Print nested list
# [[1]]
# [[1]][[1]]
# [1] 12 13 14 15 16 17 18 19 20
# 
# [[1]][[2]]
# [1] "p" "o" "n" "m" "l" "k"
# 
# [[1]][[3]]
# [1] "yyyy"
# 
# 
# [[2]]
# [[2]][[1]]
# [1] 4 5 6 7 8
# 
# [[2]][[2]]
# [1] "g" "f" "e" "d" "c" "b" "a"
# 
# [[2]][[3]]
# [1] "xxx"
# 
# 
# [[3]]
# [[3]][[1]]
# [1] "Another"
# 
# [[3]][[2]]
# [1] "list"
# 
# [[3]][[3]]
# [1] "in R"
# 
#

As you can see, we have created a nested list containing three sub-lists.

 

Example 2: Create List of Lists in for-Loop

The following R programming syntax illustrates how to append list objects to a nested list within a for-loop.

To set up the example, we first have to create a vector containing all list names of lists that we want to combine:

my_list_names <- c("list_1", "list_2", "list_3")  # Create vector of list names
my_list_names                                     # Print vector of list names
# [1] "list_1" "list_2" "list_3"

Next, we have to create an empty list to which we will insert our list objects:

my_nested_list2 <- list()                         # Create empty list
my_nested_list2                                   # Print empty list
# list()

Finally, we can run a for-loop over the vector of list names and concatenate another list to our main list using index positions and the get function:

for(i in 1:length(my_list_names)) {               # Run for-loop over lists
  my_nested_list2[[i]] <- get(my_list_names[i])
}

Let’s have a look at our list of lists:

my_nested_list2                                   # Print nested list
# [[1]]
# [[1]][[1]]
# [1] 12 13 14 15 16 17 18 19 20
# 
# [[1]][[2]]
# [1] "p" "o" "n" "m" "l" "k"
# 
# [[1]][[3]]
# [1] "yyyy"
# 
# 
# [[2]]
# [[2]][[1]]
# [1] 4 5 6 7 8
# 
# [[2]][[2]]
# [1] "g" "f" "e" "d" "c" "b" "a"
# 
# [[2]][[3]]
# [1] "xxx"
# 
# 
# [[3]]
# [[3]][[1]]
# [1] "Another"
# 
# [[3]][[2]]
# [1] "list"
# 
# [[3]][[3]]
# [1] "in R"
# 
#

As you can see, our final list consists of exactly the same sub-lists as the nested list that we have created in Example 1. However, this time we have used a for-loop to create our nested list.

Note that we could also use other types of loops such as while-loops and repeat-loops to create a nested list in R.

 

Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which shows the R programming syntax of this tutorial. You can find the video below.

 

 

Furthermore, you could have a look at some of the other tutorials on this website. You can find some articles on related topics such as data elements, extracting data, and lists below.

 

In this R programming article you have learned how to create nested lists. If you have additional questions, let me know in the comments section below.

 

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.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top