R Matrices
Matrices are the R-objects, in which the two-dimensional rectangular data set are arranged. A matrix can be built using the matrix() function.
Syntax:
matrix( data, nrow, ncol, byrow, dimensions)
Where, data: - it is the input vector will be the data elements of the matrix
nrow: - number of rows to be created
ncol: - number of columns to be created
byrow: - it is just a logical clue. If it is TRUE then the input vector elements are arranged by row.
dimname: - it is the names assigned to the rows and columns.
Example 1:
> Mat <- matrix(c(1:9), nrow = 3, ncol = 3 ) > Mat
Output:
[,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9
Example 2:
> Mat <- matrix(c(1:9), nrow = 3, ncol = 3, byrow = TRUE) > Mat
Output:
[,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9
Example 3:
# Define the column and row names. rnames = c("r1", "r2", "r3") cnames = c("c1", "c2", "c3") m <- matrix(c(1:9), nrow = 3, byrow = TRUE, dimnames = list(rnames, cnames)) print(m)
Output:
c1 c2 c3 r1 1 2 3 r2 4 5 6 r3 7 8 9
Accessing elements of a Matrix
We can access the elements of a matrix by using the column and row index of the element.
Example:
# Create the matrix. P <- matrix(c(1:12), nrow = 4, byrow = TRUE) P # Access the element at 3rd column and 1st row. P[1,3] # Access the element at 2nd column and 4th row. P[4,2] # Access only the 2nd row. P[2,] # Access only the 3rd column. P[,3]
Output:
[,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12 [1] 3 [1] 11 [1] 4 5 6 [1] 3 6 9 12
Modifying Element of a Matrix
To modify the elements of a matrix we just need to assign the value through the assignment operator in the index of the value.
Example:
# Create the matrix. P <- matrix(c(1:12), nrow = 4, byrow = TRUE) P # Modify the element at 3rd column and 1st row. P[1,2] <- 0 P
Output:
[,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12 # Modify the element at 3rd column and 1st row. P[1,2] <- 0 P [,1] [,2] [,3] [1,] 1 0 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12
Adding Row or Column in the Matrix
We can add a row or column in the matrix using rbind() and cbind() function respectively.
Example:
# Create two 2x3 matrices. m <- matrix(1:6, nrow = 2, ncol = 2) print(m) # add column m <- cbind(m, c(7, 8)) print(m) # add row m <- rbind(m, c(9, 0, 1)) print(m)
Output:
[,1] [,2] [1,] 1 3 [2,] 2 4 [,1] [,2] [,3] [1,] 1 3 7 [2,] 2 4 8 [,1] [,2] [,3] [1,] 1 3 7 [2,] 2 4 8 [3,] 9 0 1
Changing Dimension of a Matrix
We can change the dimension of a matrix through the dim() function.
Example:
# Create two 2x3 matrix. m <- matrix(1:6, nrow = 2, ncol = 3) print(m) # change to 3X2 matrix dim(m) <- c(3,2) print(m)
Output:
[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6
Transpose a Matrix
We can transpose a matrix in R with the function t().
Example:
# Create two 2x3 matrices. m <- matrix(1:9, nrow = 3, ncol = 3) m # transpose of matrix m t(m)
Output:
[,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9
Matrix Computations
In R, we can perform various mathematical operations on the matrices using the R operators. The result of the operation is also a matrix.
To perform the operation, the dimension should be the same for the matrices.
Matrix Addition and Subtraction
Example:
# Create two 2x3 matrices. m1 <- matrix(c(11:16), nrow = 2) print(m1) m2 <- matrix(c(1:6), nrow = 2) print(m2) # Add the matrices. s <- m1 + m2 cat("Result of addition","\n") print(s) # Subtract the matrices s <- m1 - m2 cat("Result of subtraction","\n") print(s)
Output:
[,1] [,2] [,3] [1,] 11 13 15 [2,] 12 14 16 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [,1] [,2] [,3] [1,] 12 16 20 [2,] 14 18 22 [,1] [,2] [,3] [1,] 10 10 10 [2,] 10 10 10
Matrix Division
Example:
# Create two 2x3 matrices. m1 <- matrix(c(11:16), nrow = 2) print(m1) m2 <- matrix(c(1:6), nrow = 2) print(m2) # Divide the matrices r <- m1 / m2 cat("Result of division","\n") print(r)
Output:
[,1] [,2] [,3] [1,] 11 13 15 [2,] 12 14 16 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 Result of division [,1] [,2] [,3] [1,] 11 4.333333 3.000000 [2,] 6 3.500000 2.666667
Matrix Multiplication
Example:
A <- matrix(c(2,3,-2,1,2,2),3,2) B <- matrix(c(2,-2,1,2,3,1),2,3) C <- B %*% A C
Output:
[,1] [,2] [1,] 1 10 [2,] 0 4
When you change the order of the multiplication the result will be different. It means AB is not equal to BA.
Example:
A <- matrix(c(2,3,-2,1,2,2),3,2) B <- matrix(c(2,-2,1,2,3,1),2,3) C <- A %*% B C
Output:
[,1] [,2] [,3] [1,] 2 4 7 [2,] 2 7 11 [3,] -8 2 -4Reference: https://www.datamentor.io/r-programming/matrix/ https://www.tutorialspoint.com/r/r_matrices.htm