[R] standardizing one variable by dividing each value by the mean - but within levels of a factor

Dimitri Liakhovitski ld7631 at gmail.com
Thu Jan 21 16:03:28 CET 2010


Thanks a lot, everyone!

On Thu, Jan 21, 2010 at 9:44 AM, hadley wickham <h.wickham at gmail.com> wrote:
> On Wed, Jan 20, 2010 at 4:37 PM, Dimitri Liakhovitski <ld7631 at 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.Liakhovitski at ninah.com



More information about the R-help mailing list