title: Sliding time intervals for time series data in R
body: <p>I am trying to extract interesting statistics for an irregular time series data set, but coming up short on finding the right tools for the job.  The tools for manipulating regularly sampled time series or index-based series of any time are pretty easily found, though I'm not having much luck with the problems I'm trying to solve.</p>
  
  <p>First, a reproducible data set:</p>
  
  <pre><code>library(zoo)
  set.seed(0)
  nSamples    &lt;- 5000
  vecDT       &lt;- rexp(nSamples, 3)
  vecTimes    &lt;- cumsum(c(0,vecDT))
  vecDrift    &lt;- c(0, rnorm(nSamples, mean = 1/nSamples, sd = 0.01))
  vecVals     &lt;- cumsum(vecDrift)
  vecZ        &lt;- zoo(vecVals, order.by = vecTimes)
  rm(vecDT, vecDrift)
  </code></pre>
  
  <p>Assume the times are in seconds.  There are almost 1700 seconds (just shy of 30 minutes) in the <code>vecZ</code> series, and 5001 entries during that time.  (NB: I'd try using <code>xts</code>, but <code>xts</code> seems to need date information, and I'd rather not use a particular date when it's not relevant.)</p>
  
  <p>My goals are the following:</p>
  
  <ul>
  <li><p>Identify the indices of the values 3 minutes before and 3 minutes after each point.  As the times are continuous, I doubt that any two points are precisely 3 minutes apart.  What I'd like to find are the points that are at most 3 minutes prior, and at least 3 minutes after, the given point, i.e. something like the following (in pseudocode):</p>
  
  <p><code>backIX(t, vecZ, tDelta) = min{ix in length(vecZ) : t - time(ix) &lt; tDelta}</code>
  <code>forwardIX(t, vecZ, tDelta) = min{ix in length(vecZ) : time(ix) - t &gt; tDelta}</code></p>
  
  <p>So, for 3 minutes, <code>tDelta = 180</code>.  If <code>t=2500</code>, then the result for <code>forwardIX()</code> would be 3012 (i.e. time(vecZ)[2500] is 860.1462, and time(vecZ)[3012] is 1040.403, or just over 180 seconds later), and the output of <code>backwardIX()</code> would be 2020 (corresponding to time 680.7162 seconds).</p>
  
  <p>Ideally, I would like to use a function that does not require <code>t</code>, as that is going to require <code>length(vecZ)</code> calls to the function, which ignores the fact that sliding windows of time can be calculated more efficiently.</p></li>
  <li><p>Apply a function to all values in a rolling window of time.  I've seen <code>rollapply</code>, which takes a fixed window size (i.e. fixed number of indices, but not a fixed window of time).  I can solve this the naive way, with a loop (or <code>foreach</code> ;-)) that is calculated per index <code>t</code>, but I wondered if there are some simple functions already implemented, e.g. a function to calculate the mean of all values in a given time frame.  Since this can be done efficiently via simple summary statistics that slide over a window, it should be computationally cheaper than a function that accesses all of the data multiple times to calculate each statistic.  Some fairly natural functions: mean, min, max, and median.</p>
  
  <p>Even if the window isn't varying by time, the ability to vary the window size would be adequate, and I can find that window size using the result of the question above.  However, that still seems to require excess calculations, so being able to specify time-based intervals seems more efficient.</p></li>
  </ul>
  
  <p>Are there packages in R that facilitate such manipulations of data in time-windows, or am I out of luck and I should write my own functions?</p>
  
  <hr>
  
  <p>Note 1: <a href="https://stackoverflow.com/questions/7571788/regular-analysis-over-irregular-time-series">This question</a> seeks to do something similar, except over disjoint intervals, rather than rolling windows of time, e.g. I could adapt this to do my analysis on every successive 3 minute block, but I don't see a way to adapt this for rolling 3 minute intervals.</p>
  
  <p>Note 2: I've found that switching from a <code>zoo</code> object to a numeric vector (for the times) has significantly sped up the issue of range-finding / window endpoint identification for the first goal.  That's still a naive algorithm, but it's worth mentioning that working with <code>zoo</code> objects may not be optimal for the naive approach.</p>