Matrix Functions in R – solve(), dim(), sum(), mean(), cbind()

In this article, we will learn what are matrix functions in R and different functions that operate on matrices. We will see their usage and look at a few examples.

If you skipped the R matrix tutorial, then revise R matrices before understanding its function.

What are R Matrices?

In R, matrices are two-dimensional data structures. They can store values of the same data type in the form of rows and columns. They have a dimattribute that defines their dimensions. We can create a matrix using the matrix() function.

What are the Matrix Functions in R?

Functions that take a matrix as input or return a matrix as output are called matrix functions. There are a lot of matrix functions in R. The major one that we are going to discuss today are:

  1. is.matrix() function
  2. %*% operator
  3. solve() function
  4. t() function
  5. dim() and dimnames() functions
  6. cbind() and rbind() functions
  7. diag() function
  8. det() function
  9. colSums(), rowSums(), and sum() functions
  10. colMeans(), rowMeans(), and mean() functions

1. is.matrix() Function

The is.matrix() function takes an object as input and returns TRUE if the object is a matrix. For example:

Code:

mat1 <- matrix(c(1:9),c(3,3))
is.matrix(mat1)

Output:

is.matrix() - matrix function in r

2. %*% Operator

We can perform the element-wise multiplication of two matrices using the * operator. But we can do matrix multiplication, with the %*% operator. For example:

Code:

mat2 <- matrix(c(3,2,0,0,0,1,2,-2,1),c(3,3))
mat1*mat2

Code:

mat1%*%mat2

Output:

%*% operator - matrix functions in r

3. solve() Function

The solve() function takes a matrix as input and returns the matrix’s inverse as output. For example:

Code:

mat2I <- solve(mat2)
mat2I

Output:

solve() - matrix functions in r

4. t() Function

The t() function in R gives us the transpose of a matrix. For example:

Code:

tmat1 <- t(mat1)
tmat1

Output:

t() - matrix functions in r

5. dim() and dimnames() Functions

The dim() function shows the dimension of a matrix. It can also change the dimension of a matrix and also convert a vector into a matrix by giving it dimensions.

The dimnames() function shows the names of the rows and columns of a matrix. It can also set or change the names of the rows and columns of a matrix. For example:

Code:

dim(mat1)

Code:

dim(mat1) <- c(9,1)
mat1

Output:

dim() - matrix functions in r

Code:

dimnames(mat2)

Code:

rnames <- c("row1","row2","row3")
cnames <- c("col1","col2","col3")
dimnames(mat2) <- list(rnames,cnames)
mat2

Output:

dimnames() - matrix functions in r

6. cbind() and rbind() Functions

The cbind() function joins two or more matrices or vectors column-wise. The rbind() function joins them row-wise. For example:

Code:

mat3 <- matrix(c(1:12),c(3,4))
mat4 <- matrix(c(11:22),c(3,4))
cbind(mat3,mat4)

Code:

rbind(mat3,mat4)

Output:

cbind() and rbind() - matrix functions in r

7. diag() Function

The diag() function can extract or replace the diagonal of a matrix and can also construct a diagonal matrix. For example:

Code:

diag(mat3)

Code:

diag(diag(mat3))

Output:

diag() - matrix functions in r

8. det() Function

The det() function returns the determinant of the input matrix. For example:

Code:

det(mat2)

Output:

det() - matrix functions in r

9. colSums(), rowSums(), and sum() Functions

The colSums() function returns the sums of each column of the matrix. The rowSums() function returns the sum of each row of a matrix. The sum() function returns the sum of all the elements of the matrix. For example:

Code:

colSums(mat3)

Code:

rowSums(mat3)

Code:

sum(mat3)

Output:

colsums(), rowsums() and sum() - matrix function in r

10. colMeans(), rowMeans(), and mean() Function

The colMeans() function shows the means of each column of a matrix. The rowMeans() function shows the means of each row of the matrix. The mean() function returns the mean of all the elements of the matrix. For example:

Code:

colMeans(mat3)

Code:

rowMeans(mat3)

Code:

mean(mat3)

Output:

colmeans(),rowmeans() and mean() - matrix functions in r

Summary

Matrices are two-dimensional, homogeneous data-structures in R. They have rows and columns and they can store data of only a single type.

In this tutorial, we learned about matrices and matrix functions in R. We looked at a few common matrix functions. We also looked at their usage and their examples.

R has a lot of useful functions to perform complex calculations on matrices. With practice, you should be able to discover some more weird quirks and behaviors of these functions.

Any queries while executing these R matrix functions?

Don’t worry! Ask below, and our TechVidvan experts will be happy to help you.🙂

Keep Executing!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.