title: Grouping every n minutes with dplyr
body: <p>I have a dataset containing 10 events occuring at a certain time on a given day, with corresponding value for each event:</p>
  
  <pre><code>d1 &lt;- data.frame(date = as.POSIXct(c("21/05/2010 19:59:37", "21/05/2010 08:40:30", 
                              "21/05/2010 09:21:00", "21/05/2010 22:29:50", "21/05/2010 11:27:34", 
                              "21/05/2010 18:25:14", "21/05/2010 15:16:01", "21/05/2010 09:41:53", 
                              "21/05/2010 15:01:29", "21/05/2010 09:02:06"), format ="%d/%m/%Y %H:%M:%S"),
                   value = c(11313,42423,64645,643426,1313313,1313,3535,6476,11313,9875))
  </code></pre>
  
  <p>I want to aggregate the results every 3 minutes, in a standard dataframe format (from "21/05/2010 00:00:00" to "21/05/2010 23:57:00", so that the dataframe has 480 bins of 3 minutes each)</p>
  
  <p>First, I create a dataframe containing bins of 3 minutes each:</p>
  
  <pre><code>d2 &lt;- data.frame(date = seq(as.POSIXct("2010-05-21 00:00:00"), 
                              by="3 min", length.out=(1440/3)))
  </code></pre>
  
  <p>Then, I merge the two dataframes together and remove NAs:</p>
  
  <pre><code>library(dplyr)
  m &lt;- merge(d1, d2, all=TRUE) %&gt;% mutate(value = ifelse(is.na(value),0,value))
  </code></pre>
  
  <p>Finally, I use <code>period.apply()</code> from the <code>xts</code> package to sum the values for each bin:</p>
  
  <pre><code>library(xts)
  a &lt;- period.apply(m$value, endpoints(m$date, "minutes", 3), sum)
  </code></pre>
  
  <p>Is there a more efficient way to do this ? It does not feel optimal.</p>
  
  <p><strong>Update #1</strong></p>
  
  <p>I adjusted my code after Joshua's answer:</p>
  
  <pre><code>library(xts)
  startpoints &lt;- function (x, on = "months", k = 1) { 
    head(endpoints(x, on, k) + 1, -1) 
  }
  
  m &lt;- seq(as.POSIXct("2010-05-21 00:00:00"), by="3 min", length.out=1440/3)
  x &lt;- merge(value=xts(d1$value, d1$date), xts(,m))
  y &lt;- period.apply(x, c(0,startpoints(x, "minutes", 3)), sum, na.rm=TRUE)
  </code></pre>
  
  <p>I wasn't aware that <code>na.rm=TRUE</code> could be used with <code>period.apply()</code>, which now allows me to skip <code>mutate(value = ifelse(is.na(value),0,value))</code>. It's a step forward and I'm actually pleased with the <code>xts</code> approach here but I would like to know if there is a <em>pure</em> <code>dplyr</code> solution I could use in such a situation.</p>
  
  <p><strong>Update #2</strong></p>
  
  <p>After trying Khashaa's answer, I had an error because my timezone was not specified. So I had:</p>
  
  <pre><code>&gt; tail(d4)
                 interval sumvalue
  476 2010-05-21 23:45:00       NA
  477 2010-05-21 23:48:00       NA
  478 2010-05-21 23:51:00       NA
  479 2010-05-21 23:54:00       NA
  480 2010-05-21 23:57:00    11313
  481 2010-05-22 02:27:00   643426
  &gt; d4[450,]
                 interval sumvalue
  450 2010-05-21 22:27:00       NA
  </code></pre>
  
  <p>Now, after <code>Sys.setenv(TZ="UTC")</code>, it all works fine.</p>