How to Calculate Determinant of Matrix in R


The determinant of a matrix is a single value that is the function of the elements of a square matrix.

The determinant is used to find the inverse of a matrix, which is used in a wide variety of tasks involving matrix algebra.

Often we use statistical programming software such as R to calculate the determinant of a matrix because it can be computationally intensive for large matrices.

The easiest way to calculate the determinant of a matrix in R is by using the det() function, which uses the following basic syntax:

#calculate determinant of matrix named 'my_matrix'
determinant <- det(my_matrix)

This returns a single value stored in a variable named determinant that represents the determinant of the matrix.

Note that the determinant of a matrix is also useful to know because it tells us if the inverse of a matrix exists or not.

If the determinant of a matrix is equal to zero, then the inverse of the matrix does not exist.

However, if the determinant of a matrix is any other number besides zero then the inverse of the matrix does exist.

Note: The det() function comes built-in with base R which means you do not need to install or load any external packages to use the function.

The following example shows how to use the det() function to calculate the determinant of a matrix in R in practice.

Related: How to Calculate Inverse Matrix in R

Example: How to Calculate Determinant of Matrix in R

Suppose we create the following matrix named my_matrix that contains 3 rows and 3 columns:

#create 3x3 matrix
my_matrix <- matrix(c(2, 5, -3, 0, 2, 6, 5, 5, 8), nrow=3)

#view matrix
my_matrix

     [,1] [,2] [,3]
[1,]    2    0    5
[2,]    5    2    5
[3,]   -3    6    8

Since this is a square matrix (same number of rows and columns) we can attempt to calculate the inverse of this matrix.

Before doing so, we can use the det() function to calculate the determinant of the matrix to ensure that it is not equal to zero:

#calculate determinant of matrix
det(my_matrix)

[1] 152

This tells us that the determinant of the matrix is equal to 152.

This also tells us that the inverse matrix exists since this value is not equal to zero.

It’s important to note that the determinant only exists for square matrices.

For example, suppose that we create the following matrix with 5 rows and 2 columns:

#create 5x2 matrix
my_matrix <- matrix(c(2, 5, -3, 0, 2, 6, 5, 5, 8, 4), nrow=5)

#view matrix
my_matrix

     [,1] [,2]
[1,]    2    6
[2,]    5    5
[3,]   -3    5
[4,]    0    8
[5,]    2    4

Now suppose that we attempt to calculate the determinant of this matrix by using the det() function:

#attempt to calculate determinant of matrix
det(my_matrix)

Error in determinant.matrix(x, logarithm = TRUE, ...) : 
  'x' must be a square matrix
Calls: det -> determinant -> determinant.matrix
Execution halted

The det() function results in an error and it tells us the exact problem: ‘x’ must be a square matrix.

Since the matrix that we created is not a square matrix, the determinant does not exist.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Sort a Matrix in R
How to Remove NA from Matrix in R
How to Convert Data Frame to Matrix in R
How to Convert a Table to a Matrix in R

Featured Posts

Leave a Reply

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