Calculate Determinant of Matrix in R (2 Examples) | det & determinant Functions
This tutorial shows how to calculate the determinant of a matrix using the det and determinant functions in the R programming language.
Table of contents:
Let’s get started…
Creation of Example Data
Have a look at the following example data:
set.seed(823764) # Create example matrix mat <- matrix(sample(1:25, 25, replace = TRUE), nrow = 5) mat # Print example matrix
Table 1 illustrates that our exemplifying data consists of five rows and five variables.
Example 1: Calculate Determinant of Matrix Using det() Function
In this section, I’ll illustrate how to apply the det function to calculate the determinant of a matrix object in R.
Have a look at the following R syntax:
det(mat) # Apply det function # [1] 152257
As you can see based on the previous output of the RStudio console, the determinant of our example matrix is 152257.
Example 2: Calculate Modulus of Determinant on Logarithm Scale Using determinant() Function
This example explains how to use the determinant function in R.
Within the determinant function, we have the option to specify whether we want to return the logarithm of the modulus of the determinant.
If we set the logarithm argument to be equal to FALSE, the same value is returned as when we use the det function:
determinant(mat, # Apply determinant function & logarithm = FALSE logarithm = FALSE) # $modulus # [1] 152257 # attr(,"logarithm") # [1] FALSE # # $sign # [1] 1 # # attr(,"class") # [1] "det"
Note that the determinant function also returns the sign of the determinant and certain attributes.
However, if we set the logarithm argument to be equal to TRUE (i.e. the default specification), the modulus of the determinant on the logarithm scale is computed:
determinant(mat) # Apply determinant function # $modulus # [1] 11.93333 # attr(,"logarithm") # [1] TRUE # # $sign # [1] 1 # # attr(,"class") # [1] "det"
Video, Further Resources & Summary
Do you want to know more about the application of the det and determinant functions? Then I recommend watching the following video instruction on my YouTube channel. I explain the content of this article in the video.
The YouTube video will be added soon.
Furthermore, you could have a look at the other posts on my website.
- Matrix Cross Product in R
- Multiply Rows of Matrix by Vector in R
- Calculate Product of Vector & Data Frame
- Cartesian Product in R
- The R Programming Language
Summary: In this tutorial you have learned how to apply the det and determinant functions in the R programming language. Let me know in the comments, if you have further questions. Furthermore, please subscribe to my email newsletter to receive regular updates on new articles.
Statistics Globe Newsletter