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

Follow TechVidvan on Google & Stay updated with latest technology trends

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!!

Did you like our efforts? If Yes, please give TechVidvan 5 Stars on Google | Facebook

1 Response

  1. Carla Villamarin says:

    Hi! Thanks for the info about matrix functions.

    When I use the function to calculate the mean of my matrix as “mean(IndexData)” I get the result NA and the message “Warning message:
    In mean.default(IndexData) :
    argument is not numeric or logical: returning NA”

    This is the str of my matrix:

    ‘data.frame’: 19 obs. of 19 variables:
    $ V1 : num 0.001 0.235 0.117 0.0695 0.3484 …
    $ V2 : num 0.235 0.001 0.0688 0.0854 0.2649 …
    $ V3 : num 0.117 0.0688 0.001 0.0982 0.1393 …
    $ V4 : num 0.0695 0.0854 0.0982 0.001 0.4855 …
    $ V5 : num 0.348 0.265 0.139 0.486 0.001 …
    $ V6 : num 0.278 0.419 0.646 0.581 0.734 …
    $ V7 : num 0.827 0.894 0.966 0.215 0.265 …
    $ V8 : num 0.175 0.278 0.279 0.161 0.296 …
    $ V9 : num 0.198 0.496 0.458 0.647 0.697 …
    $ V10: num 0.0965 0.1156 0.1793 0.385 0.2555 …
    $ V11: num 0.654 0.624 0.794 0.865 0.916 …
    $ V12: num 0.28 0.149 0.498 0.289 0.449 …
    $ V13: num 0.127 0.156 0.37 0.276 0.151 …
    $ V14: num 0.91 0.998 0.115 0.346 0.236 …
    $ V15: num 0.879 0.938 0.391 0.308 0.174 …
    $ V16: num 0.2557 0.3426 0.307 0.0967 0.3167 …
    $ V17: num 0.419 0.442 0.625 0.669 0.672 …
    $ V18: num 0.251 0.237 0.38 0.568 0.549 …
    $ V19: num 0.597 0.689 0.73 0.291 0.424 …

Leave a Reply

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