[Re: Re: [R] Scaling on a data.frame]

Stefan Roepcke stefan.roepcke at metagen.de
Wed Jun 5 11:26:44 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)"

Hey, 
finely this works (adapted from Prof. Ripleys suggestion):

grpmeans <- tapply(d$VALUE, d$FAC1, mean, na.rm=T)  
d$c <- d$VALUE - grpmeans[d$FAC1] 
grpscale <- tapply(d$c, d$FAC1, sd, na.rm=T)
d$cs <- d$c / grpscale[d$FAC1] 

It was funny to get 3 eligible answers
but non of them worked at first go.
 
1)	grpmeans <- tapply(d[1], FAC1, mean)
	d$s <- scale(d[1], grpmeans[d$FAC1], FALSE)

2)	zz <- by(d1,d1$FAC1,function(x,...){c(scale(x$VALUE),x$FAC2,x$FAC3)})

These doesn't work because scale and by doesn't return the expected
vector ???

3) split(z, g) <- lapply(split(x, g), scale)
This is something elegant - i would like to apply.
So i hurry to install R 1.5 .

On R 1.3 i got this when i tried to follow Peter Dalgaards suggestion:
> get("split<-.data.frame")
Error in get(x, envir, mode, inherits) : variable "split<-.data.frame"
was not found

Thanks again, I found a solution and learned a lot 8-)

Stefan
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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