Create Matrix of Lists in R (Example)
In this post, I’ll explain how to construct a matrix of lists in the R programming language.
Table of contents:
You’re here for the answer, so let’s get straight to the example:
Introduction of Example Data
The first step is to create some data that we can use in the examples below:
my_list1 <- list(1, 2, 3) # Create first list my_list1 # Print first list # [[1]] # [1] 1 # # [[2]] # [1] 2 # # [[3]] # [1] 3 |
my_list1 <- list(1, 2, 3) # Create first list my_list1 # Print first list # [[1]] # [1] 1 # # [[2]] # [1] 2 # # [[3]] # [1] 3
my_list2 <- list("a", "b", "c", "d") # Create second list my_list2 # Print second list # [[1]] # [1] "a" # # [[2]] # [1] "b" # # [[3]] # [1] "c" # # [[4]] # [1] "d" |
my_list2 <- list("a", "b", "c", "d") # Create second list my_list2 # Print second list # [[1]] # [1] "a" # # [[2]] # [1] "b" # # [[3]] # [1] "c" # # [[4]] # [1] "d"
my_list3 <- list(1, "xxx", 1:3) # Create third list my_list3 # Print third list # [[1]] # [1] 1 # # [[2]] # [1] "xxx" # # [[3]] # [1] 1 2 3 |
my_list3 <- list(1, "xxx", 1:3) # Create third list my_list3 # Print third list # [[1]] # [1] 1 # # [[2]] # [1] "xxx" # # [[3]] # [1] 1 2 3
my_list4 <- list(1:3, 2:5) # Create fourth list my_list4 # Print fourth list # [[1]] # [1] 1 2 3 # # [[2]] # [1] 2 3 4 5 |
my_list4 <- list(1:3, 2:5) # Create fourth list my_list4 # Print fourth list # [[1]] # [1] 1 2 3 # # [[2]] # [1] 2 3 4 5
The previous outputs of the RStudio console show our four exemplifying list objects.
At this point of the tutorial, each of these lists is stored in a different data object.
Example: Combine Lists in Matrix Using matrix() & list() Functions
This example shows how to merge multiple list objects in a single matrix.
Have a look at the following R code and its output:
my_mat <- matrix(list(my_list1, # Create matrix of lists my_list2, my_list3, my_list4), ncol = 2) my_mat # Print matrix of lists # [,1] [,2] # [1,] list,3 list,3 # [2,] list,4 list,2 |
my_mat <- matrix(list(my_list1, # Create matrix of lists my_list2, my_list3, my_list4), ncol = 2) my_mat # Print matrix of lists # [,1] [,2] # [1,] list,3 list,3 # [2,] list,4 list,2
As you can see, we have created a 2×2 matrix where each matrix cell contains one of our lists.
Video, Further Resources & Summary
Have a look at the following video on my YouTube channel. In the video, I demonstrate the R code of this page:
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.
Furthermore, you could have a look at the related R posts that I have published on my website.
- Create Matrix that Only Contains Zeros
- Convert Nested Lists to Data Frame or Matrix
- Create Random Matrix in R
- Create Empty Matrix in R
- Introduction to R
In summary: In this R tutorial you have learned how to create a matrix containing list objects. A method that can be useful when you want to create a two-dimensional array of vectors which are of different lengths. Tell me about it in the comments, if you have any additional comments or questions.
Statistics Globe Newsletter