In article <[email protected]>,
[email protected] says...
> aves = aggregate(df1$score, by=list(col1=df1$col1, col2=df1$col2), mean)
> results = merge(df1, aves)
Or, with the 'plyr' package, which has a very nice syntax:
library(plyr)
ddply(df1, .(col1, col2), transform, Average=mean(score))
It may be a bit slow for very large datasets, though.
Here's an alternative, which will be as fast as the aggregate solution.
within(df1, { Average=ave(score, col1, col2, FUN=mean) } )
Which one you use is a matter of taste.
And of course, the 'within' function is not the important part here;
'ave' is. For example, if you have attached your data frame, you just
have to type
Average=ave(score, col1, col2, FUN=mean)
--
Karl Ove Hufthammer
______________________________________________
[email protected] 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.