[R] Specifying parameters for use in "plyr" / "ddply"

Dimitri Liakhovitski ld7631 at gmail.com
Wed Apr 7 16:37:38 CEST 2010

Dear R-ers!

# I have a data frame with one factor and 2 numeric variables:

# I want to divide each value of each variable by its group mean -
using plyr and ddply. It works fine, for example, for variable "a":
x2<-ddply(x, "group", transform, a = a / mean(a, na.rm = T))

# Because I want to do the same for both variables (a and b) I want to
put it into a function.
# So, I am parametrising the grouping variable and the variable to transform.
# However, my code below is not working - I know that x[[variable]] is
not correct - but what is the right way of doing it?
x2<-ddply(x, grouping.factor, transform, x[[variable]] = x[[variable]]
/ mean(x[[variable]], na.rm = T))

Or is there a more effective way of using ddply on a bunch of variables?
Thank you very much for your advise!

Dimitri Liakhovitski
Dimitri.Liakhovitski at ninah.com

More information about the R-help mailing list