# Mastering Matrices

**R for Public Health**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R has many ways to store information. Most of the time, our data comes in the form of a dataset, which we bring into R as a data.frame object. However, there are times when we want to use matrices as well. This post will show you **how matrices can be useful and how to manipulate them easily**.

First of all, the big difference between matrices and dataframes is that all of the rows and columns of a matrix must have the same class (numeric, character, etc). In a dataframe, you can have some of each. See my initial post about objects, here.

You can **convert from one to the other using as.data.frame() or as.matrix()**. Be careful though, that if you convert a dataframe with different classes of columns, then your matrix will just be all character:

In order to have a numeric matrix, I’m going to just take the first 6 columns of the mydata dataframe. I can **delete columns of a matrix or dataframe** in two ways:

*except*the 7th column. Note that if I wanted to drop even more columns, I would just use the c() function like this:

Note now that since I have taken out the one character column in my dataframe before I convert it to a matrix, I will get a numeric matrix instead of a character matrix:

This kind of operation for deleting columns works the same way in both matrices and dataframes. However, **to add a column to a dataframe or matrix is different**. In a dataframe, you can use the $ operator to identify columns, like mydata$Married is the vector corresponding to the Married column. However, you can’t use the $ operator on matrices. You will get the following error that the “$ operator is invalid for atomic vectors”, which I see all the time when I’m converting back and forth from dataframes to matrices and make a mistake:

All this message means is that the object you’re using is a matrix (mydata.mat) and you can’t use the $ operator on a matrix. If you get this message, you can either use as.data.frame() to convert your matrix to a dataframe, or you can adjust what you are doing to accomodate the rules of matrices. **For adding columns to a matrix, you use cbind()**, and likewise for rows, rbind().

So let’s say I want to add an age squared column. In the dataframe, I do:

`mydata$agesq<-mydata$Age^2`

which instantly names the new column “agesq”. Now for a matrix, there are two ways to do this, via indexing by number or by name of the original column:

In the first line, I’m taking all rows and the second column of the mydata.mat matrix and squaring it, then I’m column binding it to the original matrix. In the second line, I’m doing the exact same thing, except that instead of indexing with a number, I can use the name of the column “Age”. I get the following after running both statements:

Notice that the last two columns of this matrix do not have names, which can be rectified, by using the colnames() function:

I don’t want to rename everything, so I take the 7th and 8th columns and name those appropriately.

Finally, what can matrices do for us? One important aspect of matrices is of course matrix multiplication, which is how we do any multivariable regression analysis. I’ll do a post soon on regression analysis by hand in R. But another reason is that **matrices are great way to store values that you return during the course of running a loop**.

For example, say I want to show how great the central limit theorem is. I’ll generate deviates from some other distribution, say the Poisson, and take the mean of the draws each time. I’ll do this 1000 times and then show what the histogram looks like. In a problem like this, I’ll use a loop. I’ll also use a matrix to store the mean each time.

Ok, we start out by initializing a matrix. We’ll create a matrix of all NAs with 1000 rows and 3 columns using the matrix() function:

So in the first line, we’re saying for each value of i going from i=1 to i=nrow(mat1), do the stuff in the loop. We could have written 1:1000, but it’s nice to leave it as nrow(mat1) since we may want to change the size of mat1 later and this way the loop will still be fine.

Next, we draw from a Poisson distribution three times, each time a larger number of draws (first 1 draw, then 10, then 100), and each time with a lambda of 1.

Finally, and this is where the matrix comes in, we’ll take the mean of each one of those vectors and store it. We will store the three values in the ith row of the mat1 matrix, filling in all three columns. In a longer way, I could have done:

and it would have come out the same, but the first way is nicer since it’s more compact. **Remember that matrices are just columns and rows of vectors, so you can always assign a vector to a row**, **as long as it’s the same length**. When you concatenate numbers (using the c() function), you make a vector, which is why it works.

Now, let’s see how the old CLT is working by plotting some histograms:

Again, with the histograms, I can plot each column at a time by subsetting the mat1 matrix:

Pretty nice! Other very helpful places to better understand matrices:

**leave a comment**for the author, please follow the link and comment on their blog:

**R for Public Health**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.