Lukas
Lukas

Reputation: 543

Apply function to all columns in matrix using column means

I have a matrix m (conisting of three columns, each consisting of integers and having a mean of 5,10, and 15 respectively):

m <- round(matrix(data=cbind(rnorm(30, 5), rnorm(30, 10), rnorm(30, 15)), nrow=30, ncol=3), 0)

Now, I would like to have a matrix of the same dimensions as m, where each value x is calculated as x minus the mean of the column in which x is found. How can this be done? I have tried applying various apply-functions (specifically, I have been looking at two questions by user3640617 and User60), but the problem seems to be that I can't use the mean of a row as an argument in sapply, lapply or vapply...

Example: If the head(m) is

     [,1] [,2] [,3]
[1,]    6   11   14
[2,]    6    8   16
[3,]    6   11   15
[4,]    6   10   17
[5,]    5    9   15
[6,]    3   10   15

I want to get

     [,1] [,2] [,3]
[1,]    1    1   -1
[2,]    1   -2    1
[3,]    1    1    0
[4,]    1    0    2
[5,]    0   -1    0
[6,]   -2    0    0

Upvotes: 0

Views: 538

Answers (2)

Onyambu
Onyambu

Reputation: 79188

you are looking for

scale(m,center=T,scale=F)[,]

another way is

sweep(m,2,colMeans(m)) 

but not as fast as the scale

Upvotes: 4

VicaYang
VicaYang

Reputation: 554

One way is to use apply(m, 2, function(x)(x-mean(x)))

Another way is to use t(t(m)-colMeans(m))

Upvotes: 5

Related Questions