• Category
• >R Programming

# Matrix Functions in R

• Lalit Salunkhe
• Aug 21, 2020 Matrices are the two-dimensional data structures in R that allow us to store the data into the form of rows and columns and have a dedicated function named matrix() that allows us to create them in the R environment.

In this article, we will go through some of the important as well as widely used matrix functions that allow us to manipulate the matrices. These functions will take a matrix as an input and generate an output accordingly.

## What the matrix functions are?

As said earlier, functions which take a matrix as an argument, do manipulating tasks (like slice and dice, matrix multiplication, transposing matrix, etc.) and then return a matrix as an output. These are the functions designed in a specific manner to work only with the matrices. Following are some of the popular matrix functions that we use.

• %*% operator for matrix multiplication.

• solve() Function.

• t() Function.

• dim() and dimnames() Function.

• cbind() and rbind() Function.

• diag() Function.

• det() Function.

• sum(), colSums(), and rowSums() Function.

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

## %*% Operator for Matrix Multiplication

The multiplication for matrices works in a different way. If you want to multiply two matrices, you need to use the %*% operator. This operator does the matrix multiplication in a proper way. To read more about how matrix multiplication works in algebra, check the link How to multiply matrices for a better realisation. Let us see an example for the matrix multiplication operator under R. Example code with output for the matrix multiplication

Note: The one thing which we should always keep in mind is that in general, the multiplication satisfies the commutative law of multiplication. In general, if we have two numbers as a and b, then a*b = b*a; but not in matrix. It doesn’t satisfy the commutative law of multiplication. Hence, for matrices A and B, AB and BA are not equal.

## The solve() Function in R

When you are working with matrices, finding the inverse is one of the biggest challenges for the user. Trying to compute the inverse of a matrix manually is a hell of a job. However, not when it comes to R. R programming has a dedicated function to do the task for you. The solve() function in R programming takes a matrix as an argument and then returns the inverse of that matrix.

Let us see an example for the solve() function in R programming: Example code with output for the solve() function

## The t() Function in R

Taking the transpose of a matrix is one of the common tasks in matrix algebra. Taking the transpose of a matrix is nothing but switching/flipping between the rows and the columns. When you do so, columns of your matrix will become the rows and rows will become the new columns. In R, we have a dedicated function t() to do the task. This function takes a matrix as an input and generates a transpose of the same as an output.

See an example below for a better understanding. Example code for the transpose t() function in R

In the example code under the above image, you can clearly see how the rows and columns are interchanged for the original matrix “x” after we use the transpose function (i.e. t() function)

## The dim() and dimnames() Function in R

When you want to check the dimension (how many rows and columns are there) of a matrix, you can use the dim() function. This function can also help you in setting new dimensions for a given matrix or allows you to convert a vector input into a matrix by setting dimensions.

On the other hand, the dimnames() function is straightforward and allows you to set the names for rows and columns (a.k.a dimensions) of your current matrix. These names are nothing but labels attached for the rows and columns of a given matrix.

See an example below where these two functions are used for a better realization. Example code with output for the dim() and the dimnames() Function in R

## The cbind() and rbind() Function in R

The cbind() function in R is a function that allows you to create or modify the matrices. The function either takes vectors or matrices as an input and then combines those as columns to create a new matrix. If the matrices are provided as an argument, it concatenates the two matrices column-wise.

On the similar lines the rbind() function in R allows you to create or modify the matrices. It either takes vectors or matrices as an input and then combines those as rows to create a new matrix. If the matrices are provided as an input, this function concatenates them row-wise.

See an example below for the cbind() and rbind() function. Example code with output for the cbind() and the rbind() function in R

## The diag() Function in R

In R, when you are in a situation where you need to create a diagonal matrix, you can use the diag() function that allows you to create a diagonal matrix. This function also helps you in extracting the diagonal of an existing matrix which already has been created.

It takes a vector as an input argument when used to generate a diagonal matrix. If used to extract the diagonal of a matrix, it takes a matrix as an argument.

See an example for the diag() function in R. Example code with output for the diag() function in R

## The det() Function in R

The det() function in R allows us to find out the determinant of a matrix. A determinant is important when it comes to the computation of inverse of the matrix in mathematical terms. Example for the det() function is as shown below: Example code with output for the det() function

## The sum(), colSums(), and rowSums() Function in R

As the names itself suggest, the sum() function computes the sum of all elements from the matrix. The colSums() function allows us to find out the column-wise sum of elements for the matrix. The rowSums() function allows us to find out the row-wise sum of the elements for the given matrix.

The example below will make you understand how these functions work in R. Example code with output for the sum(), colSums(), and rowSums() function

## The mean(), colMeans(), and rowMeans() Function in R

Same as the sum(), colSums(), and rowSums(), we have mean(), colMeans(), and rowMeans() functions in R that allows us to find out the mean value for all elements, column-wise mean values, and row-wise mean values for a given matrix in R.

Let us see an example for these functions. Example code with output for the mean(), colMeans(), and rowMeans() functions

## Conclusion

In this article, we have seen some of the most widely used matrix functions. The functions which can be used for matrix manipulations.

• The %*% is not a function but an operator that allows you to do matrix multiplication. However, it is included here because it is one of the important parts of matrix manipulation.

• The solve(), t(), and dim(), diag(), and det() functions are considered as matrix manipulation functions. These functions allow us to take a matrix as an argument and then returns the inverse, transpose, dimension, diagonal, and determinant of the given matrix.

• cbind() and rbind() functions allow us to either create a matrix from given vector inputs or concatenate two or more matrices column-wise and row-wise respectively.

• The dimnames() function allows us to get the names for rows and columns for the given matrix.

• The sum(), colSums(), rowSums(), mean(), colMeans(), and rowMeans() functions are the functions which generate the basic statistics for the given matrix either for all elements, column-wise, or row-wise.  