Skip to content
Prev 215776 / 398500 Next

Code is too slow: mean-centering variables in a data framebysubgroup

Dimitri,

You might try applying ave() to each column.  E.g., use

f2 <- function(frame) {
   for(i in 2:ncol(frame)) {
      frame[,i] <- ave(frame[,i], frame[,1],
FUN=function(x)x/mean(x,na.rm=TRUE))
   }
   frame
}

Note that this returns a data.frame and retains the
grouping column (the first) while your original
code returns a matrix without the grouping column.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
100),b=rnorm(1