Do matrix arithmetic in R.
Join DataCamp today, and start our interactive intro to R programming tutorial for free: https://www.datacamp.com/courses/free-introduction-to-r
Throughout the exercises on matrices, you have already bumped into the `colSums()` and `rowSums()` functions. The `colSums()` function, for example, took the sum of each column, and stored the result in a vector. Apart from these matrix-specifc math functions, you can also do standard arithmetic with functions. Remember how you could do all the typical arithmetic operations on vectors? Again, it's exactly the same thing for matrices: all computations are done element-wise.
Ever heard about that other classic trilogy, the Lord of the Rings? As for Star Wars, you can build a matrix of box office revenue for both US and non-US regions. The information is saved in the the matrix, lotr_matrix, which has been constructed as follows:
These are astronomical numbers, we're talking about millions of US dollars here. What if you wanted to convert this to Euros? At the time of writing, 1 euro converts to 1 point 12 US dollars, so to convert the figures to euros, we'll have to divide the figures by 1.12. We can simply use the division operator as if we were performing the division on a single number:
R performs the operation element-wise: each element is divided by 1.12. This works just the same for multiplication, summation and subtraction. Say, for example, that the theaters world-wide claim 50 million dollars on the box office revenue. How much is left for the Lord of the rings concern itself then? We simply subtract 50 from LOTR matrix:
Operating on matrices with single numbers looks pretty straightforward, and this actually also holds when you're performing calculations with two matrices. Suppose that instead of demanding the same sum of money for every release, theaters worldwide ask for 50 million for the first release, 80 for the second and 100 for the third. We could build a matrix, say, `theater_cut`, with the same dimensions as LOTR matrix, that looks as follows
If we now subtract theater_cut from LOTR matrix:
This time, the substraction was also performed element wise: the figures for the Two towers lowered by 80, while the figures for the return of the king were lowered by 100. Makes perfect sense right?
Now, what would happen if we use a vector, containing 50, 80 and 100 to subtract from the lord of the rings matrix? Let's try it out.
The result is exactly the same! That's because once again, R performed recycling. R realizes that the dimensions of the matrix and the vector don't match. Therefore, the vector is extended to a matrix of the same size, and is filled up with the vector elements column by column. The matrix that is thus actually subtracted from the lotr matrix is the following:
This matrix is the exact same one as we built manually before, so the result is the same. Again, blindly trusting R that it performs recycling just the way you want it can be quite a dangerous practice. You should be fully aware of how this recycling is actually happening.
If you are familiar with linear algebra, you might wonder how matrix multiplication would work. Well, in R, multiplication is simply performed element wise. Suppose you want to convert the US dollar figures to euros with the exchange rate at the time of the release, you can again create a new matrix, this time with the amount of euros you should pay for 1 dollar.
Now, we can simply multiply the lord of the rings matrix with the rates matrix:
Again, every calculation is done element-by-element. The top left figure has been multiplied by one point 11, while the bottom right figure was multiplied by point 82.
To end this last video on matrices, I want to stress once more that matrices and vectors are very similar: they simply are data structures that can store elements of the same type. The vector does this in a one-dimensional sequence, while the matrix uses a two-dimensional grid structure. Both of them perform coercion when you want to store elements of different types and both of them perform recycling when necessary. Similarly, vector and matrix arithmetic are straightforward: all calculations are performed element-wise. That's all folks. Time to wrap up on matrices in the following exercises and I'll be awaiting you to explain all about factors!