How to Use Standard Operations in a Matrix in R
Probably the strongest feature of R is its capability to deal with complex matrix operations in an easy and optimized way. Because much of statistics boils down to matrix operations, it’s only natural that R loves to crunch those numbers.
When talking about operations on matrices, you can treat either the elements of the matrix or the whole matrix as the value you operate on. That difference is pretty clear when you compare, for example, transposing a matrix and adding a single number (or scalar) to a matrix.
When transposing, you work with the whole matrix. When adding a scalar to a matrix, you add that scalar to every element of the matrix.
You add a scalar to a matrix simply by using the addition operator, +, like this:
> first.matrix + 4 [,1] [,2] [,3] [,4] [1,] 5 8 11 14 [2,] 6 9 12 15 [3,] 7 10 13 16
You can use all other arithmetic operators in exactly the same way to perform an operation on all elements of a matrix.
The difference between operations on matrices and elements becomes less clear if you talk about adding matrices together. In fact, the addition of two matrices is the addition of the responding elements. So, you need to make sure both matrices have the same dimensions.
Let’s look at another example: Say you want to add 1 to the first row, 2 to the second row, and 3 to the third row of the matrix first.matrix. You can do this by constructing a matrix second.matrix that has four columns and three rows and that has 1, 2, and 3 as values in the first, second, and third rows, respectively.
The following command does so using the recycling of the first argument by the matrix function:
> second.matrix <- matrix(1:3, nrow=3, ncol=4)
With the addition operator, you can add both matrices together, like this:
> first.matrix + second.matrix [,1] [,2] [,3] [,4] [1,] 2 5 8 11 [2,] 4 7 10 13 [3,] 6 9 12 15
This is the solution your math teacher would approve of if she asked you to do the matrix addition of the first and second matrix. And even more, if the dimensions of both matrices are not the same, R will complain and refuse to carry out the operation, as shown in the following example:
> first.matrix + second.matrix[,1:3] Error in first.matrix + second.matrix[, 1:3] : non-conformable arrays
But what would happen if instead of adding a matrix, you added a vector? Take a look at the outcome of the following code:
> first.matrix + 1:3 [,1] [,2] [,3] [,4] [1,] 2 5 8 11 [2,] 4 7 10 13 [3,] 6 9 12 15
Not only does R not complain about the dimensions, but it recycles the vector over the values of the matrices. In fact, R treats the matrix as a vector in this case by simply ignoring the dimensions. So, in this case, you don’t use matrix addition but simple (vectorized) addition.
By default, R fills matrices column-wise. Whenever R reads a matrix, it also reads it column-wise. This has important implications for the work with matrices. If you don’t stay aware of this, R can bite you in the leg nastily.