[R] standardizing one variable by dividing each value by the mean - but within levels of a factor
Dimitri Liakhovitski
ld7631 at gmail.com
Wed Jan 20 23:37:49 CET 2010
Hello!
I have a data frame with a factor and a numeric variable:
x<-data.frame(factor=c("b","b","d","d","e","e"),values=c(1,2,10,20,100,200))
For each level of "factor" - I would like to divide each value of
"values" by the mean of "values" that corresponds to the level of
"factor"
In other words, I would like to get a new variable that is equal to:
1/1.5
2/1.5
10/15
20/15
100/150
200/150
I realize I could do it through tapply starting with:
factor.level.means<-tapply(x$values,x$factor,mean) ... etc.
But it seems clunky to me.
Is there a more elegant way of doing it?
Thanks a lot!
--
Dimitri Liakhovitski
More information about the R-help
mailing list