DDPLY to weighed by classes across dataframe R
I'm new to plyr and want to take the weighted mean of values within a
class to reshape a dataframe for multiple variables. Using the following
code, I know how to do this for one variable, such as x2:
set.seed(123)
frame <- data.frame(class=sample(LETTERS[1:5], replace = TRUE),
x=rnorm(20), x2 = rnorm(20), weights=rnorm(20))
ddply(frame, .(class),function(x) data.frame(weighted.mean(x$x2, x$weights)))
However, I would like the code to create a new data frame for x and x2
(and any amount of variables in the frame). Does anybody know how to do
this? Thanks
No comments:
Post a Comment