How to Create a List of Multiple Matrices in R (Example Code)

In this article, I’ll show how to combine multiple matrices in a list object in the R programming language.

Introduction of Exemplifying Data

my_matrix_a <- matrix(LETTERS[10:21],    # First list
                      nrow = 3)
my_matrix_a
#      [,1] [,2] [,3] [,4]
# [1,] "J"  "M"  "P"  "S" 
# [2,] "K"  "N"  "Q"  "T" 
# [3,] "L"  "O"  "R"  "U"
my_matrix_b <- matrix(101:120,           # Second list
                      nrow = 5)
my_matrix_b
#      [,1] [,2] [,3] [,4]
# [1,]  101  106  111  116
# [2,]  102  107  112  117
# [3,]  103  108  113  118
# [4,]  104  109  114  119
# [5,]  105  110  115  120

Example: Insert Multiple Matrices into New List

list_of_matrices <- list(my_matrix_a,    # Store matrices in list
                         my_matrix_b)
list_of_matrices                         # Final output
# [[1]]
#      [,1] [,2] [,3] [,4]
# [1,] "J"  "M"  "P"  "S" 
# [2,] "K"  "N"  "Q"  "T" 
# [3,] "L"  "O"  "R"  "U" 
# 
# [[2]]
#      [,1] [,2] [,3] [,4]
# [1,]  101  106  111  116
# [2,]  102  107  112  117
# [3,]  103  108  113  118
# [4,]  104  109  114  119
# [5,]  105  110  115  120

Further Resources & Related Articles

In addition, you might want to read the related articles on this website. You can find some interesting articles 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.
You need to agree with the terms to proceed

Menu
Top