# 6 ways of mean-centering data in R

##### Posted on January 15, 2014

One of the most frequent operations in multivariate data analysis is the so-called mean-centering. In this post, I’ll show you six different ways to mean-center your data in R.

## Mean-centering

Prior to the application of many multivariate methods, data are often pre-processed. This pre-processing involves transforming the data into a suitable form for the analysis. Among the different pre-treatment procedures, one of the most common operations is the well known mean-centering.

Mean-centering involves the subtraction of the variable averages from the data. Since multivariate data is typically handled in table format (i.e. matrix) with columns as variables, mean-centering is often referred to as column centering.

What we do with mean-centering is to calculate the average value of each variable and then subtract it from the data. This implies that each column will be transformed in such a way that the resulting variable will have a zero mean.

### Algebraic standpoint

Algebraically, data-centering can be seen as a transformation. This transformation is done using what we could name as a matrix-centering operator denoted by $$H$$

where $$I$$ is the identity matrix of size $$n$$, $$1$$ is a vector of ones of length $$n$$, and $$n$$ is the number of rows in the data $$X$$. If we premultiply $$X$$ by $$H$$ we get the centered matrix $$X_c$$:

Equivalently, if we denote by $$\bar{x}$$ the vector of column-averages, the mean-centered matrix can be calculated as:

From a geometric point of view, data-centering is just a traslation or repositioning of the coordinate system. In other words, the mean-centering procedure corresponds to moving the origin of the coordinate system to coincide with the average point.

## Mean-cenetring in R

Data can be mean-centered in R in several ways, and you can even write your own mean-centering function. I’ll discuss six different ways to do it. More interestingly, we’ll compare those six options to see which one is the fastest.

For illustration purposes we’ll use the following random small dataset:

### Using the scale function

Perhaps the most simple, quick and direct way to mean-center your data is by using the function scale(). By default, this function will standardize the data (mean zero, unit variance). To indicate that we just want to subtract the mean, we need to turn off the argument scale = FALSE.

### Using the apply function

Center can be done with the apply() function. In this case, the idea is to remove the mean on each column. This is done by declaring a function (inside apply()) that performs the mean-centering operation:

### Using the sweep function

Another interesting option to mean-center our data is by using the function sweep(). This function has some similarities with apply(). If you give sweep() a value (i.e. a summary statistic) and a function, it will sweep it out on the indicated margin (either by rows or by columns). In our case, the statistic to sweep out by columns is the mean of every variable.

### Using the colMeans function

Other function that we can take advantage of colMeans(). With this function we can calculate the average value of each column; then we can construct a matrix with the averages, which will be subtracted from the data:

### Using a center-operator

We can create the mean-center operator $$H$$ and use it to premultiply the data matrix like so:

### Using mean subtraction

Finally, we can write a function that implements the algebraic alternative in which the mean-vector is used to create the mean-matrix that is subtracted from the data:

### What option to use?

So far we’ve seen six different ways in which we can mean-center a matrix. But what option is the more efficient (computationally)? To get an answer nothing better than make a small contest to see which of our 6 options is the fastest (and which one the slowest):

And the winner is … center_colmeans()

Published in categories how-to  Tagged with mean  center  R