Get startedGet started for free

Matrix - a 2D vector

1. Matrix - a 2D vector

An extension of the idea of a vector is to take it to 2 dimensions. That is, to give it rows and columns. This structure is known as a matrix.

2. Enter the matrix

We can create a simple matrix in R using the matrix function. Here, we pass in a vector, but then specify how many rows and columns that the matrix will have. Like vectors, we can save matrices in a variable for later use. By default, it will fill _down_ first before moving to the next column. To alter this so that R fills across each row first, we can specify an additional argument, byrow, and set it to TRUE. One thing to note about matrices is that, like vectors, you can only put one type of data in it. This means that you cannot have a numeric column and a character column in the same vector. Later, we will explore a data structure where you can have different types in each column.

3. Matrix coercion

If you attempt to do this with a matrix, it will be coerced to the highest ranking data type. Here, the matrix coerce_me has been changed to a character matrix because of the character "hi".

4. cbind( ) and rbind( )

While the matrix function is great, you will often want to create a matrix by binding together existing vectors into one structure. For example, consider these two vectors of stock prices for Microsoft and Ebay. They are for the same time period, so wouldn't it make sense to have them in columns side by side? To do this, we can use cbind. cbind allows us to pass in multiple vectors of the same length, and it will bind them as columns in a single matrix. Alternatively, to bind the vectors as rows, you can use rbind. Notice how the names of the variables become the corresponding row or column names in the matrix. In finance, one is often interested in the relationship between two stocks. Specifically, it would be nice to have a measure of how much two stocks move relative to each other. If Microsoft's stock goes up, is it likely that Ebay's stock will also go up? One way to try and capture this relationship is through a measure called

5. cor()relation

correlation. In its simplest form, correlation is just a number from -1 to 1, where 1 is a perfect positive linear relationship, -1 is a perfect negative linear relationship, and 0 implies no linear relationship at all. The scatterplot shown here shows data with a correlation of point-908. The data is tightly clustered together along a positive linear path.

6. cor()relation

Let's use R to calculate the correlation between Microsoft and Ebay for this small data set. You can use the cor function, and pass in the vectors for microsoft and ebay, and it will return a single number showing the correlation between the two stocks. Alternatively, you can give cor a matrix containing the two stocks, and it will return a correlation matrix. This is a matrix containing the correlations for all possible pairs in your data. When you practice with correlations, you will see how useful this idea is if you have 3 or more stocks. Note that the 1 in the upper left corner of the correlation matrix is the correlation of microsoft with itself. Intuitively, it should move perfectly linear with itself, so it makes sense that this is 1.

7. Let's practice!

That seems like enough for now. Have fun practicing with matrices!