title: Compute monthly average from daily data within groups in R
body: <p>I am trying to compute the monthly average from daily data for each group within the data.  In this example, there are 3 groups each with daily observations from 2011-01-01 to 2011-03-31</p>
  
  <pre><code>times &lt;- c(31,28,31)
  date = rep(seq(as.Date('2011-01-01'),as.Date('2011-03-31'),by = 1),
         times=3)
  id = rep(rep(1001:1003, times), each=3)
  val = rnorm(length(id), mean=5, sd=2)
  df &lt;- data.frame(date, id, val)
  
  &gt; head(df)
    date   id      val
  1 2011-01-01 1001 6.341471
  2 2011-01-02 1001 4.353585
  3 2011-01-03 1001 8.131239
  4 2011-01-04 1001 3.761434
  5 2011-01-05 1001 6.344846
  6 2011-01-06 1001 7.068889   
  
  
  &gt; tail(df)
        date   id      val
  265 2011-03-26 1003 5.644132
  266 2011-03-27 1003 4.949719
  267 2011-03-28 1003 4.490786
  268 2011-03-29 1003 1.739529
  269 2011-03-30 1003 2.250610
  270 2011-03-31 1003 1.853057
  </code></pre>
  
  <p>The desired output should be something like this with the computed monthly values:  </p>
  
  <pre><code>monthYear  id    monthlyValue
  2011-01    1001  ?
  2011-02    1001  ?
  2011-03    1001  ?
  ....       ....  ..
  2011-01    1003  ?
  2011-02    1003  ?
  2011-03    1003  ?
  </code></pre>