A Matrix is a collection of data values in 2 dimensions of the same basic data type, R creates a matrix of values using a matrix() function.
It is similar to a vector and has one additional dimension attribute. We can check the R Matrices class using the class() function and ensure that the variable is a matrix or not.
Creating a Matrix in R
See the basic syntax for creating a matrix in R using the matrix() function.
matrix(vector,nrow,ncol,byrow)
vector- a variable of vector datatype
nrow- the number of rows in a matrix
ncol- the number of column in a matrix
byrow- a boolean value, if it is true then input vector values will be arranged by row
See this example of creating a matrix
- #Create a matrix using Vector
 - test_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
 - #Print matrix on console
 - print(test_matrix)
 
and output is
- [,1] [,2]
 - [1,] 1 2
 - [2,] 3 4
 
Accessing values of a Matrix
We can access an entire row, column or any specific value using row and column index of the matrix. See this example here to get an idea about it
- #Create a matrix using Vector
 - test_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
 - #Print 1st row on console
 - print(test_matrix[1,])
 - #Print 2nd column on console
 - print(test_matrix[,2])
 - #Print 1st column of 2nd row on console
 - print(test_matrix[2,1])
 
and output is like this
Performing Arithmetic Operations on R Matrices
We can perform addition, subtraction, multiplication and division on the matrices that have the same dimension.
See this example.
- #Create two matrix using Vector
 - first_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
 - sec_matrix<-matrix(c(7,8,9,10,11,12), nrow=2, ncol=2,byrow= TRUE)
 - #First Matrix
 - print(first_matrix)
 - #Second Matrix
 - print(sec_matrix)
 - #Addition
 - print(first_matrix + sec_matrix)
 - #Subtraction
 - print(first_matrix - sec_matrix)
 - #Multiplication
 - print(first_matrix * sec_matrix)
 - #Division
 - print(first_matrix / sec_matrix)
 
and see the output on the R console
Add a new row or column in the existing Matrix
We can add a new row using rbind() and a column using cbind() function in an existing matrix.
See this example
- #Create a matrix using Vector
 - first_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
 - #First Matrix
 - print(first_matrix)
 - #Adding new row in matrix
 - v <- c(89,90,91)
 - first_matrix <- rbind(first_matrix,v)
 - print(first_matrix)
 - #Adding new column in matrix
 - v <- c(92,93,94)
 - first_matrix <- cbind(first_matrix,v)
 - print(first_matrix)
 
and output is
Combining Matrices in R
We can combine the column of two matrices that have the same number of rows using cbind() function.
- #Create two matrix using Vector
 - first_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
 - sec_matrix<-matrix(c(7,8,9,10,11,12), nrow=2, ncol=2,byrow= TRUE)
 - #First Matrix
 - print(first_matrix)
 - #Second Matrix
 - print(sec_matrix)
 - #Combining two matrices
 - print(cbind(first_matrix,sec_matrix))
 
the output on R Console is
Transpose Matrix
We can create the transpose matrix of a matrix using t() function.
- #Create a matrix using Vector
 - first_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
 - #First Matrix
 - print(first_matrix)
 - #Transpose matrix
 - print(t(first_matrix))
 
The output on R Console is
Cheers  Happy Learning