In article <800acfc0-2c3c-41f1-af18-3b52f7e43...@jhsph.edu>, 
bcarv...@jhsph.edu 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

______________________________________________
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.

Reply via email to