[R] subtract the mean from each column

Chuck Cleland ccleland at optonline.net
Thu Apr 10 19:15:24 CEST 2008


On 4/10/2008 1:03 PM, kayj wrote:
> Hi,
> 
> I am new to R an dI need some help
> I have a matrix of real values 100*300 and I would like to calculate the
> mean for each column , then for each entry in a column i need to subtract
> the mean so I will have a matrix where the columns have zero mean. any one
> know how to do that . Thanks  

mymat <- matrix(runif(10*4), ncol=4)

colMeans(mymat)
[1] 0.4711083 0.4825977 0.4757448 0.3037873

newmat <- apply(mymat, 2, scale, scale=FALSE, center=TRUE)

colMeans(newmat)
[1]  1.110223e-17 -2.220446e-17 -1.110223e-17  1.110223e-17

?scale

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list