Thanks a lot, everyone! On Thu, Jan 21, 2010 at 9:44 AM, hadley wickham <h.wick...@gmail.com> wrote: > On Wed, Jan 20, 2010 at 4:37 PM, Dimitri Liakhovitski <ld7...@gmail.com> > wrote: >> 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? > > Here's one way with the plyr package: > > library(plyr) > ddply(x, "factor", transform, scaled = values / mean(values, na.rm = T)) > > Hadley > > -- > http://had.co.nz/ >
-- Dimitri Liakhovitski Ninah.com dimitri.liakhovit...@ninah.com ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.