# Error in solve.default(mat) : Lapack routine dgesv: system is exactly singular: U[2,2] = 0

**Data Science Tutorials**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Error in solve.default(mat) : Lapack routine dgesv: system is exactly singular: U[2,2] = 0 appeared first on Data Science Tutorials

What do you have to lose?. Check out Data Science tutorials here Data Science Tutorials.

Error in solve.default(mat) Lapack routine dgesv system is exactly singular: Lapack routine dgesv: system is exactly singular: U[2,2] = 0

When attempting to utilize the solution() method on a singular matrix that lacks a matrix inverse, an error like this one will appear.

This lesson explains how to fix this mistake practically.

### How to Make the Error in solve.default(mat) Lapack routine dgesv system is exactly singular Again

Let’s say we use R to build the matrix shown below.

Let’s create a singular matrix

mat <- matrix(c(1, 1, 1, 1), ncol=2, nrow=2)

Now we can view the matrix

mat [,1] [,2] [1,] 1 1 [2,] 1 1

Now imagine that we try to compute the matrix inverse using the solution() function.

Top 10 online data science programs – Data Science Tutorials

invert matrix try

solve(mat) Error in solve.default(mat) : Lapack routine dgesv: system is exactly singular: U[2,2] = 0

The absence of an inverse matrix in the matrix we generated results in an error.

Please have a look at this Wolfram MathWorld article that provides ten examples of matrices without inverse matrices.

A matrix is considered singular by definition if its determinant is zero.

Before attempting to invert a particular matrix, you can find its determinant using the det() function:

Let’s compute the matrix’s determinant

det(mat) [1] 0

Our matrix’s determinant is zero, which explains why we encounter a problem.

## How to correct the issue

Simply making a matrix that is not single is the only way to correct this issue.

Consider using R’s solution() function to invert the matrix shown below:

Now we can generate a non-singular matrix

mat <- matrix(c(1, 7, 4, 2), ncol=2, nrow=2)

let’s view the matrix

mat [,1] [,2] [1,] 1 4 [2,] 7 2

Now we can calculate the determinant of the matrix.

Add Significance Level and Stars to Plot in R (datasciencetut.com)

det(mat) [1] -26

Let’s try to invert the matrix

solve(mat) [,1] [,2] [1,] -0.07692308 0.15384615 [2,] 0.26923077 -0.03846154

The matrix is not singular, so when we invert it we don’t encounter any errors.

Hypothesis Testing Examples-Quick Overview – Data Science Tutorials

The post Error in solve.default(mat) : Lapack routine dgesv: system is exactly singular: U[2,2] = 0 appeared first on Data Science Tutorials

Learn how to expert in the Data Science field with Data Science Tutorials.

**leave a comment**for the author, please follow the link and comment on their blog:

**Data Science Tutorials**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.