Matrix Operations in R

Matrix Operations in R

  • R
  • 4 mins read

In this article, we will study the various matrix operations in R that can be performed on an object in R. 

As discussed in the previous post about matrices in R, they are well-organized data objects. Each data element in a matrix is known as a cell. There are various types of manipulations that can be performed on the cells. Some of these are:

Calculating rows and columns in a Matrix in R

Every matrix is represented by a specific number of rows and columns. The order of the matrix is rows * columns. The number of rows can be computed by the nrow() method and the number of columns by the ncol() method, respectively. Below is an example of rows and columns matrix operations in R:

#declaring a matrix in R
mat1 <- matrix(1:8, nrow = 2)
#printing the matrix contents
print(mat1)

#calculating number of rows
cat("matrix rows : ",nrow(mat1))

#calculating number of columns
cat("matrix rows : ",ncol(mat1))
The code produces the following output  :
     [,1] [,2] [,3] [,4]
[1,]    1    3    5    7
[2,]    2    4    6    8
matrix rows :  2
matrix rows :  4

Arithmetic operations in R

Two matrices can serve as operands for arithmetic operations in R language. The cells may be multiplied or divided, added to, or subtracted from each other. The result of all these operations is also a matrix. 

The following points need to be considered for different types of mathematical operations : 

  1. In the cases of addition, subtraction, power, and division, the order of the matrices should be the same for both the involved matrices. 
  2. In the case of multiplication, the columns of the first matrix must be equal to the rows of the second matrix. 
  3. Matrix operations are applied to the corresponding positions of the matrices. 
  4. These operations are applicable to complex matrices as well as real matrices. 

The following code snippet illustrates the procedures of addition, subtraction, division, and power matrix operations in R: 

#declaring a matrix in R
mat1 <- matrix(1:8, nrow = 2)
#printing the matrix contents
print(mat1)

#declaring another matrix 
mat2 <- matrix(9:16, nrow = 2)
#printing the matrix contents
print(mat2)

print("Addition")
print(mat1+mat2)

print("Subtraction")
print(mat1-mat2)

print("Division")
print(mat1/mat2)

print("Power")
print(mat1^mat2)

print("Multiplication")
print(mat1*mat2)
The code produces the following output : 
     [,1] [,2] [,3] [,4]
[1,]    1    3    5    7
[2,]    2    4    6    8
     [,1] [,2] [,3] [,4]
[1,]    9   11   13   15
[2,]   10   12   14   16
[1] "Addition"
     [,1] [,2] [,3] [,4]
[1,]   10   14   18   22
[2,]   12   16   20   24
[1] "Subtraction"
     [,1] [,2] [,3] [,4]
[1,]   -8   -8   -8   -8
[2,]   -8   -8   -8   -8
[1] "Division"
          [,1]      [,2]      [,3]      [,4]
[1,] 0.1111111 0.2727273 0.3846154 0.4666667
[2,] 0.2000000 0.3333333 0.4285714 0.5000000
[1] "Power"
     [,1]     [,2]        [,3]         [,4]
[1,]    1   177147  1220703125 4.747562e+12
[2,] 1024 16777216 78364164096 2.814750e+14

[1] "Multiplication"
     [,1] [,2] [,3] [,4]
[1,]    9   33   65  105
[2,]   20   48   84  128

Calculating the sums associated with matrix elements in R

Computing column sums of the matrix can be done using the colSums() method. The result of this method is a vector containing the sums of individual columns of a matrix in order. 

#declaring a matrix in R
mat1 <- matrix(1:8, nrow = 2)
#printing the matrix contents
print(mat1)

#calculating column sums of the matrix
cat("Column sums : ", colSums(mat1))
The code produces the following output : 
     [,1] [,2] [,3] [,4]
[1,]    1    3    5    7
[2,]    2    4    6    8
Column sums :  3 7 11 15

Computing column sums of the matrix can be done using the rowSums() method. The result of this method is a vector containing the sums of individual rows of a matrix in order. 

#declaring a matrix in R
mat1 <- matrix(1:8, nrow = 2)
#printing the matrix contents
print(mat1)
#calculating row sums of the matrix
cat("Row sums : ", rowSums(mat1))
The code produces the following output : 
     [,1] [,2] [,3] [,4]
[1,]    1    3    5    7
[2,]    2    4    6    8
> #calculating row sums of the matrix
> cat("Row sums : ", rowSums(mat1))
Row sums :  16 20

Computing the sum of the matrix can be done using the sum() method, which takes the entire matrix operations in R as a parameter. 

#declaring a matrix in R
mat1 <- matrix(1:8, nrow = 2)
#printing the matrix contents
print(mat1)
#calculating row sums of the matrix
cat("Matrix sum : ", sum(mat1))
The code produces the following output : 
     [,1] [,2] [,3] [,4]
[1,]    1    3    5    7
[2,]    2    4    6    8
Matrix sum :  36