[R] zero center a group of variables

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Apr 17 22:57:19 CEST 2002

"Héctor Villalobos" <hvillalo at ipn.mx> writes:

> A simple question, perhaps to simple, but I am new to R:
> For a data frame with k variables and n1+n2 observations (two 
> groups) how can i substract the group means for every variable, so 
> both group variables become centered to mean zero?
> Thanks in advance for your help
> Héctor

in 1.5.0, we'll have

 split(x, g) <- lapply(split(x, g), scale, scale=FALSE)

Till then, you'll have to do something like

x[g==1,] <- scale(x[g==1,], scale=FALSE)
x[g==2,] <- scale(x[g==2,], scale=FALSE)

or, more general

for ( i in split(1:length(g), g) )
   x[i,] <- scale(x[i,], scale=FALSE)

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list