R Split Matrix by Columns into List of Vectors (Example Code)

In this article, I’ll illustrate how to divide a matrix into a list of column-vectors in the R programming language.

Exemplifying Data

my_matrix <- matrix(letters[1:20],    # Example matrix in R
                    nrow = 5)
my_matrix                             # Show example matrix in RStudio
#      [,1] [,2] [,3] [,4]
# [1,] "a"  "f"  "k"  "p" 
# [2,] "b"  "g"  "l"  "q" 
# [3,] "c"  "h"  "m"  "r" 
# [4,] "d"  "i"  "n"  "s" 
# [5,] "e"  "j"  "o"  "t"

Example: Applying split Function to Divide Matrix into List of Vectors

my_list <- split(my_matrix,           # Splitting columns of matrix into list
                  rep(1:ncol(my_matrix),
                      each = nrow(my_matrix)))
my_list                               # Show list of vectors in RStudio
# $`1`
# [1] "a" "b" "c" "d" "e"
# 
# $`2`
# [1] "f" "g" "h" "i" "j"
# 
# $`3`
# [1] "k" "l" "m" "n" "o"
# 
# $`4`
# [1] "p" "q" "r" "s" "t"
#

Further Resources & Related Articles

Below, you can find some further resources on topics such as vectors, matrices, and numeric values.

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