# Mastering Matrices

April 7, 2013
By

(This article was first published on R for Public Health, and kindly contributed to R-bloggers)

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:

`mydata.mat<-as.matrix(mydata[,1:6])mydata.mat<-as.matrix(mydata[,-7])`
These two lines are doing the exact same thing. In the first, I am subsetting the dataframe mydata by taking all rows and the first 6 columns of the dataframe, then I’m converting that subset to a matrix. In the second, I’m taking all rows and all columns except the 7th column. Note that if I wanted to drop even more columns, I would just use the c() function like this:
`mydata.mat<-as.matrix(mydata[,c(-3,-7)])`

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:

`mydata.mat<-cbind(mydata.mat, mydata.mat[,2]^2)mydata.mat<-cbind(mydata.mat, mydata.mat[,"Age"]^2)`

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:

`colnames(mydata.mat)[7:8]<-c("AgeSq", "AgeSqAgain")`

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:

`mat1<-matrix(NA, nrow=1000, ncol=3)`

Next, we’ll set up the for() loop. Let’s look at it first and then go through the logic:

`for(i in 1:nrow(mat1)){`
`  vec1<-rpois(1,1)  vec2<-rpois(10,1)  vec3<-rpois(100,1)  mat1[i,]<-c(mean(vec1), mean(vec2), mean(vec3)) }`

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:

`mat1[i,1]<-mean(vec1)mat1[i,2]<-mean(vec2)mat1[i,3]<-mean(vec3)`

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:

`par(mfrow=c(1,3))hist(mat1[,1], main="n=1")hist(mat1[,2], main="n=10")hist(mat1[,3], main="n=100")`

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:

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...