set.seed(123)
# Create a numeric matrix
<- matrix(sample(9), nrow = 3, ncol = 3)
mat print(mat)
[,1] [,2] [,3]
[1,] 3 2 7
[2,] 6 8 1
[3,] 9 5 4
Essential Operations and Applications
Raju Rimal
December 2, 2024
March 19, 2025
Matrices are fundamental data structures in R, ideal for handling two-dimensional data. This blog will guide you through creating, manipulating, and performing various operations on matrices in R, complete with numerous examples. Whether you’re a beginner or an experienced R user, you’ll find something valuable here!
A matrix in R is a two-dimensional, rectangular data structure where all elements must be of the same type (numeric, character, or logical). Think of it as a collection of vectors arranged in rows and columns.
matrix()
The matrix()
function is the simplest way to create a matrix.
rbind()
or cbind()
You can convert a vector into a matrix by setting its dimensions using dim()
.
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
Use indices to access elements, rows, or columns of a matrix.
You can perform element-wise operations directly on matrices.
Use %*%
for matrix multiplication.
Transpose a matrix using the t()
function.
[1] -243
[,1] [,2] [,3]
[1,] -0.1111111 -0.11111111 0.22222222
[2,] 0.0617284 0.20987654 -0.16049383
[3,] 0.1728395 -0.01234568 -0.04938272
Use apply()
to perform operations on rows or columns.
Add rows or columns to an existing matrix.
Create an identity matrix with diag()
.
Generate matrices with random numbers.
[,1] [,2] [,3]
[1,] 0.4533342 0.1029247 0.04205953
[2,] 0.6775706 0.8998250 0.32792072
[3,] 0.5726334 0.2460877 0.95450365
[,1] [,2] [,3]
[1,] 1.2240818 0.1106827 0.4978505
[2,] 0.3598138 -0.5558411 -1.9666172
[3,] 0.4007715 1.7869131 0.7013559
eigen() decomposition
$values
[1] 35.473352 -6.541417 -5.178552 4.246617
$vectors
[,1] [,2] [,3] [,4]
[1,] 0.3914594 -0.6682313 -0.83892483 0.48333070
[2,] 0.4333322 -0.3286765 -0.16776284 -0.80395643
[3,] 0.4894401 -0.1857571 0.07737131 0.34609112
[4,] 0.6476350 0.6410405 0.51193207 -0.01632281
$d
[1] 36.039191 8.057178 6.073354 2.893603
$u
[,1] [,2] [,3] [,4]
[1,] -0.3945895 -0.09791418 -0.861480899 -0.30424099
[2,] -0.4543558 -0.53481886 0.460781405 -0.54333236
[3,] -0.5065236 -0.36518921 -0.002329039 0.78106676
[4,] -0.6174899 0.75565762 0.213367602 -0.04649824
$v
[,1] [,2] [,3] [,4]
[1,] -0.4063220 0.09522161 0.3775443 0.8266170
[2,] -0.3815022 0.24970384 0.7077947 -0.5395653
[3,] -0.3510749 0.79269889 -0.4970054 -0.0368852
[4,] -0.7523994 -0.54791360 -0.3308664 -0.1556059
Reshape matrices using array()
.
, , 1
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
, , 2
[,1] [,2]
[1,] 7 10
[2,] 8 11
[3,] 9 12
Solve Ax = b
using solve()
.
Matrices are widely used to store and manipulate datasets in statistics and machine learning.
data.frame
or tibble
.Matrices are versatile and essential in R for numerical computations and data handling. From basic operations to advanced applications, mastering matrices opens the door to solving complex problems effectively. Practice with the examples above and experiment to deepen your understanding of this powerful R data structure!