Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't believe that there's any way to do this built into zoo or chron, but you can create your own function by using a little math. Here you go:</p> <pre><code>trunc.minutes &lt;- function (x, n.minutes) { if (!inherits(x, "times")) x &lt;- as.chron(x) x &lt;- as.numeric(x) sec &lt;- round(24 * 3600 * abs(x - floor(x))) h &lt;- (sec%/%(n.minutes*60))/(60/n.minutes) hour &lt;- as.integer(h) minutes &lt;- (h %% hour) * 60 chron(dates=chron::dates(x), times=times(paste(hour, minutes, "00", sep=":"))) } </code></pre> <p>Here's an example of the usage:</p> <pre><code>dts &lt;- chron::dates(c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92")) tms &lt;- times(c("23:03:20", "23:29:56", "01:03:30", "18:21:03", "16:56:26")) x &lt;- chron(dates = dts, times = tms) # original dates x [1] (02/27/92 23:03:20) (02/27/92 22:29:56) (01/14/92 01:03:30) [4] (02/28/92 18:21:03) (02/01/92 16:56:26) trunc.minutes(x, 15) # new dates at 15 minute intervals [1] (02/27/92 23:00:00) (02/27/92 22:15:00) (01/14/92 01:00:00) [4] (02/28/92 18:15:00) (02/01/92 16:45:00) trunc.minutes(x, 30) # new dates at 30 minute intervals [1] (02/27/92 23:00:00) (02/27/92 22:00:00) (01/14/92 01:00:00) [4] (02/28/92 18:00:00) (02/01/92 16:30:00) </code></pre> <p>Lastly, you can now use this function to aggregate the data:</p> <pre><code>ts.zoo &lt;- zoo(rnorm(5), x) # a zoo time series </code></pre> <p>Or just use these new dates for aggregation (see how it rolls up the second example since there are two values in that window):</p> <pre><code>&gt; aggregate(ts.zoo, trunc.minutes(x, 15), mean) (01/14/92 01:00:00) (02/01/92 16:45:00) (02/27/92 23:00:00) (02/27/92 23:15:00) -0.6738659 -0.4844803 0.7968155 -1.3571121 (02/28/92 18:15:00) 0.7625861 &gt; aggregate(ts.zoo, trunc.minutes(x, 30), mean) (01/14/92 01:00:00) (02/01/92 16:30:00) (02/27/92 23:00:00) (02/28/92 18:00:00) -0.6738659 -0.4844803 -0.2801483 0.7625861 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload