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 matrix calculate determinant 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.

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top