How to Transform Vector to Matrix in R (Example Code)
In this article, I’ll illustrate how to switch from vector to matrix class in the R programming language.
Example Data
my_vector <- LETTERS[1:10] # Constructing example vector in R my_vector # Showing example vector in RStudio console # [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" |
my_vector <- LETTERS[1:10] # Constructing example vector in R my_vector # Showing example vector in RStudio console # [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
Example: Transform Vector to Matrix Object
my_matrix <- matrix(my_vector, # Converting vector to matrix nrow = 2) my_matrix # Showing matrix in RStudio console # [,1] [,2] [,3] [,4] [,5] # [1,] "A" "C" "E" "G" "I" # [2,] "B" "D" "F" "H" "J" |
my_matrix <- matrix(my_vector, # Converting vector to matrix nrow = 2) my_matrix # Showing matrix in RStudio console # [,1] [,2] [,3] [,4] [,5] # [1,] "A" "C" "E" "G" "I" # [2,] "B" "D" "F" "H" "J"
Further Resources & Related Tutorials
Please find some related tutorials on topics such as matrices, variables, lists, and indices below: