[R] Scaling on a data.frame

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Jun 4 11:31:52 CEST 2002

Stefan Roepcke <stefan.roepcke at metagen.de> writes:

> Hey,
> hopefully there is an easy way to solve my problem.
> All that i think off is lengthy and clumsy.
> Given a data.frame d with columns VALUE, FAC1, FAC2, FAC3.
> Let FAC1 be something like experiment number,
> so that there are exactly the same number of rows for each level of FAC1
> in the data.frame.
> Now i would like to scale all values according to the center of its
> experiment.
> So i can apply s <- by(d[1], FAC1, scale).
> But i don't want to lose the binding to the other factors (FAC2, FAC3).
> Or can i only control it via order preserving - it seems unsave.
> Something like that would be nice:
> "by(d[1], FAC1) <- by(d[1], FAC1, scale)"

R-1.5.0 has split<- which does this sort of thing. If you have an
older version, you should of course upgrade, but the definitions are
not very complicated:

> get("split<-.data.frame")
function (x, f, value) 
    x[unlist(split(seq(length = nrow(x)), f)), ] <- do.call("rbind", 
> get("split<-.default")
function (x, f, value) 
    x[unlist(split(seq(along = x), f))] <- unlist(value)

The usage is like

split(z, g) <- lapply(split(x, g), scale)

   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