In general, a Matrix is a rectangular array of elements arranged in rows and columns. But, the Matrix in R is a R object which contains the elements arranged in rectangular 2D fashion.

In R, you can either create a Matrix using characters or logic values. Even though you cannot find the numerous applications, you can use it for multiple computational purposes.

Let’s start with the Syntax

Matrix: A Matrix is a R object where elements is arranges in a 2D rectangular fashion.



Data = Input vectors having elements which can be given as input.

nrow = Number of rows to create.

ncol = Number of columns to create.

byrow = If TRUE, input vector elements are arranged in rows.

dimname = Names to be assigned to cols and rows.

Create a Matrix in R using Vector elements

Well, it’s time for you to create a simple matrix using the vector elements. In this section, we are going to create a vector that includes some values, and let’s try to create a matrix bypassing the vector elements as input values.

Why Wait? Let’s Roll!!!

input_values <-c(1,2,3,4,5,6,7,8,9)

This will be our input vector having values from 1 to 9. Now, how can you create a 3×3 matrix using these input values? Confused? Worry Not!

Let’s use the Matrix function in R to create a 3×3 matrix. Let’s see how it works.

matrix(input_values,nrow = 3,ncol = 3,byrow = T)

Yes, this piece of code will create a 3×3 matrix with the input vector elements as well. As shown in the syntax, our first parameter will be the input data followed by the number of rows and columns you want for the specific input data. The brow parameter, if turned TRUE, will arrange the values by rows as shown here. 

       [,1]  [,2]  [,3]
[1,]     1     2     3
[2,]     4     5     6
[3,]     7     8     9

In the same way, if you turn the byrow parameter as FALSE, then the input values are arranges by columns.

matrix(input_values,nrow = 3,ncol = 3,byrow = F)
       [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

As you can clearly see that, when you turn byrow as FALSE, then it arranges the values by columns as shown in the output.

Accessing the Matrix Elements

Till now, we have spent quite good time with creating the matrix combinations using the vector elements as the input values.

Now, what if you want to pull off some values out of the matrix? Well, accessing the elements is much important when you work with matrices.

Let’s see how it works.

For this illustration, you have to create a matrix with me with quite a different approach.

#coloumn and row names
row_names <- c('Row1','Row2','Row3')
col_names <- c('Col1','Col2','Col3')
#create a matrix 
new_matrix <- matrix(c(1:9),nrow = 3,ncol = 3,byrow = T,dimnames = list(row_names,col_names))

#display the matrix 
      Col1  Col2 Col3
Row1    1    2    3
Row2    4    5    6
Row3    7    8    9

Well, you have now created a 3×3 matrix with the row and column names. Now, let’s see how you can pull the specific value out of it.

#outputs foe the above code pieces 

Just by mentioning the row and column position you can easily extract or access the values from the matrix as shown above.

Matrix Computations

Yeah, it’s time for some computations using the matrix. You can easily perform matrix addition, subtraction, multiplication, and division as well.

One of the appealing aspects is that the resultant data will be a matrix. But keep in mind that, for matrix computations, the dimensions must be the same for both matrices.

Without spending much time, let’s do some computations using the matrix. Let’s start by creating 2 matrices that we are going to use for our mathematical computations.

Matrix Addition:

#creating 2 matrix for the computations 
matrix_one<-matrix(c(1:9),nrow = 3,ncol = 3,byrow = T)
matrux_two<-matrix(c(9:1),nrow = 3,ncol = 3,byrow = F)
      [,1] [,2] [,3]
[1,]   10    8    6
[2,]   12   10    8
[3,]   14   12   10

YAY!!! You have successfully added two matrices of different elements. It is very easy to add two matrix as shown above.

Matrix Subtraction:

#substracts the 2 matrix 
      [,1] [,2] [,3]
[1,]   -8   -4    0
[2,]   -4    0    4
[3,]    0    4    8

The values present in both matrix got subtracted with each other and you can see the output results above.

Matrix Multiplication:

#substracts the 2 matrix 
      [,1] [,2] [,3]
[1,]    9   12    9
[2,]   32   25   12
[3,]   49   32    9

You can easily multiply two matrices using the ‘*’ sign in R programming.

Matrix Division:

#substracts the 2 matrix 
          [,1]      [,2]    [,3]
[1,]   0.1111111 0.3333333    1
[2,]   0.5000000 1.0000000    3
[3,]   1.0000000 2.0000000    9

Matrix in R, I mean, the matrix computations in R is very much easy and can be performed with ease.

Wrapping Up – Matrix in R

The matrix in R is the 2D rectangular arrangement of the values. The values must be a character or logical. You can create a matrix in R, access the elements from the matrix in R, and even perform mathematical computations in R.

R is for you. You can do tons of things here. It is fast and saves plenty of execution time when it comes to computations.

I hope, by now, you got a better understanding of the Matrix in R programming. That’s all for now. Keep Matrixing!!!

More read: R documentation

Categorized in:

Tagged in:

, ,