Thanks a lot for your helpful suggestion! Dimitri On Wed, Jan 20, 2010 at 5:56 PM, Chuck Cleland <cclel...@optonline.net> wrote: > On 1/20/2010 5:37 PM, Dimitri Liakhovitski 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? > >> with(x, ave(x=values, factor, FUN=function(x){x/mean(x)})) > [1] 0.6666667 1.3333333 0.6666667 1.3333333 0.6666667 1.3333333 > > ?ave > >> Thanks a lot! > > -- > Chuck Cleland, Ph.D. > NDRI, Inc. (www.ndri.org) > 71 West 23rd Street, 8th floor > New York, NY 10010 > tel: (212) 845-4495 (Tu, Th) > tel: (732) 512-0171 (M, W, F) > fax: (917) 438-0894 >
-- 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.