Matrix is an object with elements arranged as a two-dimensional array like a table. An R matrix can contain elements of the same atomic types.
R Matrices Tutorials
- R – Create Matrix
- R – Check if R Object is a Matrix
- R – Get Element at Given Row, Column of Matrix
- R – Get Specific Row and Column of Matrix
- R – Get Multiple Rows and Columns of Matrix
- R – Multiplication of Matrix
- R – Transpose Matrix
- R – Inverse Matrix
- R – Correlation Matrix
Create Matrix
Syntax: matrix(data, nrow, ncol, byrow, dimnames)
where:
- data – values to enter
- nrow – number of rows
- ncol – number of columns
- byrow – value of ‘true’ will be assigned by rows instead of default (by column)
- dimnames – names of rows and columns
Example:
#Arranging elements sequentially by row.
m <- matrix(c(1:12), nrow = 4, byrow = TRUE)
print(m)
# Arranging elements sequentially by column.
n <- matrix(c(1:12), nrow = 4, byrow = FALSE)
print(n)
# Defining the column and row names.
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")
m1 <- matrix(c(1:12), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
print(m1)
#output:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
col1 col2 col3
row1 1 2 3
row2 4 5 6
row3 7 8 9
row4 10 11 12
Check if R Object is a Matrix
Syntax: is.matrix()
Example:
m1 <- c(1, 3, 5, 7, 9, 11)
m2 <- matrix(m1, nrow = 3, byrow = TRUE)
if (is.matrix(m1)) {
print("m1 is a matrix")
} else {
print("m1 is not a matrix")
}
if (is.matrix(m2)) {
print("m2 is a matrix")
} else {
print("m2 is not a matrix")
}
#output:
[1] "m1 is not a matrix"
[1] "m2 is a matrix"
Get Element of Matrix
Syntax: matrix[row, column]
Example:
m1 <- c(1, 3, 5, 7, 9, 11)
m2 <- matrix(m1, nrow = 3, byrow = TRUE)
print(m2)
element <- m2[2, 2]
print(element)
#output:
[,1] [,2]
[1,] 1 3
[2,] 5 7
[3,] 9 11
[1] 7
Get Specific Row and Column of Matrix
Syntax: matrix[row, ] (get specific row)matrix[ ,column] (get specific column)
Example 1 – get specific row:
m <- matrix(c(1, 3, 5, 7, 9, 11), nrow = 3, byrow = TRUE)
row <- m[2, ]
print("Matrix m")
print(m)
print("Row")
print(row)
#output:
[1] "Matrix m"
[,1] [,2]
[1,] 1 3
[2,] 5 7
[3,] 9 11
[1] "Row"
[1] 5 7
Example 2 – get specific column:
m <- matrix(c(1, 3, 5, 7, 9, 11), nrow = 3, byrow = TRUE)
col <- m[,2]
print("Matrix m")
print(m)
print("col")
print(col)
#output:
[1] "Matrix m"
[,1] [,2]
[1,] 1 3
[2,] 5 7
[3,] 9 11
[1] "col"
[1] 3 7 11
Get Multiple Rows and Columns of Matrix
Syntax: matrix[rows, ] (get multiple row)matrix[ ,columns] (get multiple column)
Example 1 – get multiple rows
#get row 1 and row 3 of matrix
m <- matrix(c(1, 3, 5, 7, 9, 11), nrow = 3, byrow = TRUE)
#we pass a vector c(1,3)
rows <- m[c(1,3), ]
print("Matrix m")
print(m)
print("Rows")
print(rows)
#output:
[1] "Matrix m"
[,1] [,2]
[1,] 1 3
[2,] 5 7
[3,] 9 11
[1] "Rows"
[,1] [,2]
[1,] 1 3
[2,] 9 11
Example 2 – get multiple columns of matrix
#get column 1 and column 3 of matrix
m <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, byrow = TRUE)
#we pass a vector c(1,3)
cols <- m[,c(1,3)]
print("Matrix m")
print(m)
print("cols")
print(cols)
#output:
[1] "Matrix m"
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[1] "cols"
[,1] [,2]
[1,] 1 3
[2,] 4 6
[3,] 7 9
Matrix Multiplication
Syntax: matrix A %*% matrix B
where the number of columns in matrix A must equal the number of rows in matrix B.
Example:
m <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, byrow = TRUE)
n <- matrix(c(0, 2, 4, 6, 8, 10, 12, 14, 16), nrow = 3, byrow = TRUE)
mn <- m %*% n
print("Matrix m")
print(m)
print("Matrix n")
print(n)
print("Result m*n")
print(mn)
#output:
[1] "Matrix m"
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[1] "Matrix n"
[,1] [,2] [,3]
[1,] 0 2 4
[2,] 6 8 10
[3,] 12 14 16
[1] "Result m*n"
[,1] [,2] [,3]
[1,] 48 60 72
[2,] 102 132 162
[3,] 156 204 252
Transpose Matrix
We call t() function to transpose a matrix, and pass given matrix as argument. The function returns the transpose of matrix.
Syntax: t()
Example:
M <- matrix(c(1, 3, 5, 7, 9, 11), nrow = 3, byrow = TRUE)
M_T <- t(M)
print("Matrix M")
print(M)
print("Transpose of M")
print(M_T)
#output:
[1] "Matrix M"
[,1] [,2]
[1,] 1 3
[2,] 5 7
[3,] 9 11
[1] "Transpose of M"
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
Inverse Matrix
We call solve() function, and pass given matrix as argument to create inverse marix. The function returns the inverse of the matrix.
Syntax: solve()
where the matrix must be square form.
Example:
M <- matrix(c(1, 2, 4, 4, 5, 3, 3, 2, 2), nrow = 3, byrow = TRUE)
M_I <- solve(M)
print("Matrix M")
print(M)
print("Inverse Matrix of M")
print(M_I)
#output:
[1] "Matrix M"
[,1] [,2] [,3]
[1,] 1 2 4
[2,] 4 5 3
[3,] 3 2 2
[1] "Inverse Matrix of M"
[,1] [,2] [,3]
[1,] -0.18181818 -0.1818182 0.6363636
[2,] -0.04545455 0.4545455 -0.5909091
[3,] 0.31818182 -0.1818182 0.1363636
Correlation Matrix
We call cor() function, and pass given matrix as argument to find Correlation Matrix in R. The function returns the Correlation Matrix for the matrix.
Syntax: cor()
Example:
M <- matrix(c(1, 5, 3, 4, 12, 6, 17, 8, 7), nrow = 3, byrow = TRUE)
M_C <- cor(M)
print("Matrix M")
print(M)
print("Correlation Matrix of M")
print(M_C)
#output:
[1] "Matrix M"
[,1] [,2] [,3]
[1,] 1 5 3
[2,] 4 12 6
[3,] 17 8 7
[1] "Correlation Matrix of M"
[,1] [,2] [,3]
[1,] 1.00000000 0.09486111 0.8095933
[2,] 0.09486111 1.00000000 0.6611431
[3,] 0.80959331 0.66114309 1.0000000
Conclusion:
We have done learning how to create matrix, get element of matrix, multiple matrix, transpose matrix, inverse matrix, and get correlation matrix.
