Inverse of Matrix in R (Example)
In this tutorial, I’ll show how to invert a matrix in R.
The article consists of this:
Let’s dive right into the examples…
Creating Example Data
First, we need to create an example matrix in R:
my_matrix <- matrix(c(4, 7, 3, 6), ncol = 2) my_matrix # [,1] [,2] # [1,] 4 3 # [2,] 7 6
The previous output of the RStudio console shows the structure of our example matrix.
Step 1: Compute Inverse of Matrix
solve(my_matrix) # [,1] [,2] # [1,] 2.000000 -1.000000 # [2,] -2.333333 1.333333
The previous output shows the values of the inverted matrix.
Step 2: Multiply Matrix by its Inverse (Identity Matrix)
If we want to check the result of Step 1, we can multiply our original matrix with the inverted matrix to check whether the result is the identity matrix. Have a look at the following R code:
solve(my_matrix) %*% my_matrix # [,1] [,2] # [1,] 1 0 # [2,] 0 1
As you can see, the RStudio console returned the identity matrix. Looks good!
Video & Further Resources
Do you need more information on the contents of this article? Then you might want to watch the following video of the Statistics Globe YouTube channel. I illustrate the R programming syntax of this tutorial in the video:
In addition, you could read the other tutorials of this homepage. A selection of articles is listed below:
To summarize: In this article, I explained how to get the inverse of a 2×2 data table in R programming. Let me know in the comments section, in case you have any further questions.