# QR Matrix Decomposition in R (Example) | Factorization with qr() Function

In this article, I’ll show how to perform a QR matrix decomposition (also called QR factorization or QU factorization) **using the qr() function** in R programming.

Table of contents:

Here’s the step-by-step process…

## Creation of Exemplifying Data

I use the following data matrix as basement for this tutorial:

set.seed(9823633) # Create random example matrix my_mat <- matrix(runif(12), nrow = 4) my_mat # Print example matrix |

set.seed(9823633) # Create random example matrix my_mat <- matrix(runif(12), nrow = 4) my_mat # Print example matrix

As you can see based on Table 1, our example data is a matrix composed of four rows and three variables.

For this tutorial, we also have to create a vector:

my_vec <- 1:4 # Create vector my_vec # Print vector # [1] 1 2 3 4 |

my_vec <- 1:4 # Create vector my_vec # Print vector # [1] 1 2 3 4

The previous output shows our vector in the RStudio console. It consists of four integer elements ranging from 1 to 4.

## Example: QR Decomposition of Matrix Using qr() & solve() Functions

The following code explains how to use conduct a QR decomposition in R.

For this task, we have to apply the solve and qr functions as shown below:

solve(qr(my_mat), my_vec) # Apply qr() & solve() # [1] 3.0006847 0.6967126 5.2930560 |

solve(qr(my_mat), my_vec) # Apply qr() & solve() # [1] 3.0006847 0.6967126 5.2930560

The previous output shows our result, i.e. a vector containing three numeric values.

## Video, Further Resources & Summary

In case you need more info on the topics of this post, you could have a look at the following video on the Statistics Globe YouTube channel. In the video, I’m illustrating the content of this page:

*The YouTube video will be added soon.*

Furthermore, you might have a look at some of the related tutorials on this website.

- Solve System of Equations in R
- Multiply Rows of Matrix by Vector in R
- Inverse of Matrix in R
- Useful Functions in R
- R Programming Language

At this point you should have learned how to **apply the qr() function** in R programming. If you have further questions or comments, please let me know in the comments section below. Furthermore, please subscribe to my email newsletter in order to receive updates on new tutorials.

### Statistics Globe Newsletter