# R Error in solve – Lapack routine dgesv: system is exactly singular (2 Examples)

In this tutorial, I’ll show how to avoid the “Error in solve.default() : Lapack routine dgesv: system is exactly singular” in R.

## Example 1: Replicating the Error in solve – Lapack routine dgesv: system is exactly singular

```mat_a <- matrix(0, # Matrix with same values ncol = 5, nrow = 5) mat_a # Show matrix in RStudio console # [,1] [,2] [,3] [,4] [,5] # [1,] 0 0 0 0 0 # [2,] 0 0 0 0 0 # [3,] 0 0 0 0 0 # [4,] 0 0 0 0 0 # [5,] 0 0 0 0 0```
```solve(mat_a) # Trying to apply solve function # Error in solve.default(mat_a) : # Lapack routine dgesv: system is exactly singular: U[1,1] = 0```

## Example 2: Avoiding the Error in solve – Lapack routine dgesv: system is exactly singular

```set.seed(397465) # Set seed mat_b <- matrix(sample(1:25), # Matrix with random values ncol = 5, nrow = 5) mat_b # Show matrix in RStudio console # [,1] [,2] [,3] [,4] [,5] # [1,] -0.08265355 0.02207009 0.009216743 0.072617198 0.04056193 # [2,] 0.08580629 0.01392880 -0.016432500 -0.106580092 -0.01300147 # [3,] 0.02421112 -0.03733198 -0.024660869 -0.003980426 0.05276354 # [4,] 0.04611424 -0.03239550 0.059035810 -0.057414554 -0.03053725 # [5,] 0.01323936 0.02474705 0.003867316 0.031638735 -0.06080249```
`solve(mat_b) # Solving matrix`