How to Convert a Factor in R
How to Create a Two-Way Data Table with R
How to Distinguish Data Types in R

How to Do Matrix Arithmetic in R

Apart from the classical arithmetic operators, R contains a large set of operators and functions to perform a wide set of matrix operations. Many of these operations are used in advanced mathematics, so you may never need them. Some of them can come in pretty handy, though, if you need to flip around data or you want to calculate some statistics yourself.

Transpose a matrix in R

Flipping around a matrix so the rows become columns and vice versa is very easy in R. The t() function (which stands for transpose) does all the work for you:

> t(first.matrix)
   [,1] [,2] [,3]
[1,]  1  2  3
[2,]  4  5  6
[3,]  7  8  9
[4,]  10  11  12

You can try this with a vector, too. As matrices are read and filled column-wise, it shouldn’t come as a surprise that the t() function sees a vector as a one-column matrix. The transpose of a vector is, thus, a one-row matrix:

> t(1:10)
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]  1  2  3  4  5  6  7  8  9  10

You can tell this is a matrix by the dimensions. This information seems trivial by the way, but imagine you’re selecting only one row from a matrix and transposing it. Unlike what you would expect, you get a row instead of a column:

> t(first.matrix[2,])
   [,1] [,2] [,3] [,4]
[1,]  2  5  8  11

Invert a matrix in R

Contrary to your intuition, inverting a matrix is not done by raising it to the power of –1, R normally applies the arithmetic operators element-wise on the matrix. So, the command first.matrix^(-1) doesn’t give you the inverse of the matrix; instead, it gives you the inverse of the elements. To invert a matrix, you use the solve() function, like this:

> square.matrix <- matrix(c(1,0,3,2,2,4,3,2,1),ncol=3)
> solve(square.matrix)
   [,1]    [,2]    [,3]
[1,] 0.5 -0.8333333 0.1666667
[2,] -0.5 0.6666667 0.1666667
[3,] 0.5 -0.1666667 -0.1666667

Be careful inverting a matrix like this because of the risk of round-off errors. R computes most statistics based on decompositions like the QR decomposition, single-value decomposition, and Cholesky decomposition. You can do that yourself using the functions qr(), svd(), and chol(), respectively. Check the respective Help pages for more information.

Multiply two matrices in R

The multiplication operator (*) works element-wise on matrices. To calculate the inner product of two matrices, you use the special operator %*%, like this:

> first.matrix %*% t(second.matrix)
   [,1] [,2] [,3]
[1,]  22  44  66
[2,]  26  52  78
[3,]  30  60  90

You have to transpose the second.matrix first; otherwise, both matrices have non-conformable dimensions. Multiplying a matrix with a vector is a bit of a special case; as long as the dimensions fit, R will automatically convert the vector to either a row or a column matrix, whatever is applicable in that case. You can check for yourself in the following example:

> first.matrix %*% 1:4
[1,]  70
[2,]  80
[3,]  90
> 1:3 %*% first.matrix
   [,1] [,2] [,3] [,4]
[1,]  14  32  50  68
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Install and Load CRAN Packages in R
How to Put Arguments in Your R Functions
How to Unload CRAN Packages in R
How to Add Titles and Axis Labels to a Plot in R
How to Evaluate the Differences in Your Data with R