Thank you all for your responses, i have now achieved the desired output for my own real data using your suggestions. I will also have to look into this 'plyr' package as i have noticed that it gets mentioned a lot.
On 21 Oct, 13:33, Karl Ove Hufthammer <k...@huftis.org> wrote: > 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-h...@r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ 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.