[R] How to average subgroups in a dataframe? (not sure how to apply aggregate(..))

2009-10-21 Thread Tony Breyal
Dear all, Lets say I have the following data frame: set.seed(1) col1 - c(rep('happy',9), rep('sad', 9)) col2 - rep(c(rep('alpha', 3), rep('beta', 3), rep('gamma', 3)),2) dates - as.Date(rep(c('2009-10-13', '2009-10-14', '2009-10-15'),6)) score=rnorm(18, 10, 3) df1-data.frame(col1=col1,

Re: [R] How to average subgroups in a dataframe? (not sure how to apply aggregate(..))

2009-10-21 Thread Benilton Carvalho
aves = aggregate(df1$score, by=list(col1=df1$col1, col2=df1$col2), mean) results = merge(df1, aves) b On Oct 21, 2009, at 9:03 AM, Tony Breyal wrote: Dear all, Lets say I have the following data frame: set.seed(1) col1 - c(rep('happy',9), rep('sad', 9)) col2 - rep(c(rep('alpha', 3),

Re: [R] How to average subgroups in a dataframe? (not sure how to apply aggregate(..))

2009-10-21 Thread Chuck Cleland
On 10/21/2009 7:03 AM, Tony Breyal wrote: Dear all, Lets say I have the following data frame: set.seed(1) col1 - c(rep('happy',9), rep('sad', 9)) col2 - rep(c(rep('alpha', 3), rep('beta', 3), rep('gamma', 3)),2) dates - as.Date(rep(c('2009-10-13', '2009-10-14', '2009-10-15'),6))

Re: [R] How to average subgroups in a dataframe? (not sure how to apply aggregate(..))

2009-10-21 Thread Karl Ove Hufthammer
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),

Re: [R] How to average subgroups in a dataframe? (not sure how to apply aggregate(..))

2009-10-21 Thread Tony Breyal
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