Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label R data types. Show all posts
Showing posts with label R data types. Show all posts

Monday 17 September 2018

R Matrices – Create , Access, Transpose, Combine Matrices in R


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

  1. #Create a matrix using Vector
  2. test_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
  3. #Print matrix on console
  4. print(test_matrix)

and output is

  1. [,1] [,2]
  2. [1,] 1 2
  3. [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

  1. #Create a matrix using Vector
  2. test_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
  3. #Print 1st row on console
  4. print(test_matrix[1,])
  5. #Print 2nd column on console
  6. print(test_matrix[,2])
  7. #Print 1st column of 2nd row on console
  8. 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.

  1. #Create two matrix using Vector
  2. first_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
  3. sec_matrix<-matrix(c(7,8,9,10,11,12), nrow=2, ncol=2,byrow= TRUE)
  4. #First Matrix
  5. print(first_matrix)
  6. #Second Matrix
  7. print(sec_matrix)
  8. #Addition
  9. print(first_matrix + sec_matrix)
  10. #Subtraction
  11. print(first_matrix - sec_matrix)
  12. #Multiplication
  13. print(first_matrix * sec_matrix)
  14. #Division
  15. 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

  1. #Create a matrix using Vector
  2. first_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
  3. #First Matrix
  4. print(first_matrix)
  5. #Adding new row in matrix
  6. v <- c(89,90,91)
  7. first_matrix <- rbind(first_matrix,v)
  8. print(first_matrix)
  9. #Adding new column in matrix
  10. v <- c(92,93,94)
  11. first_matrix <- cbind(first_matrix,v)
  12. 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.

  1. #Create two matrix using Vector
  2. first_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
  3. sec_matrix<-matrix(c(7,8,9,10,11,12), nrow=2, ncol=2,byrow= TRUE)
  4. #First Matrix
  5. print(first_matrix)
  6. #Second Matrix
  7. print(sec_matrix)
  8. #Combining two matrices
  9. 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.

  1. #Create a matrix using Vector
  2. first_matrix<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=2,byrow= TRUE)
  3. #First Matrix
  4. print(first_matrix)
  5. #Transpose matrix
  6. print(t(first_matrix))

The output on R Console is


Cheers ðŸ™‚ Happy Learning