title: R - Faster Way to Calculate Rolling Statistics Over a Variable Interval
body: <p>I'm curious if anyone out there can come up with a (faster) way to calculate rolling statistics (rolling mean, median, percentiles, etc.) over a variable interval of time (windowing). </p>
  
  <p>That is, suppose one is given randomly timed observations (i.e. not daily, or weekly data, observations just have a time stamp, as in ticks data), and suppose you'd like to look at center and dispersion statistics that you are able to widen and tighten the interval of time over which these statistics are calculated. </p>
  
  <p>I made a simple for loop that does this. But it obviously runs very slow (In fact I think my loop is still running over a small sample of data I set up to test its speed). I've been trying to get something like ddply to do this - which seems straitforward to get to run for daily stats - but I can't seem to work my way out of it. </p>
  
  <p>Example: </p>
  
  <p><strong>Sample Set-up:</strong> </p>
  
  <pre><code>df &lt;- data.frame(Date = runif(1000,0,30))
  df$Price &lt;- I((df$Date)^0.5 * (rnorm(1000,30,4)))
  df$Date &lt;- as.Date(df$Date, origin = "1970-01-01")
  </code></pre>
  
  <p><strong>Example Function (that runs really slow with many observations</strong></p>
  
  <pre><code>SummaryStats &lt;- function(dataframe, interval){
    # Returns daily simple summary stats, 
    # at varying intervals
    # dataframe is the data frame in question, with Date and Price obs
    # interval is the width of time to be treated as a day
  
    firstDay &lt;- min(dataframe$Date)
    lastDay  &lt;- max(dataframe$Date)
    result &lt;- data.frame(Date = NULL,
                         Average = NULL,  Median = NULL,
                         Count = NULL,
                         Percentile25 = NULL, Percentile75 = NULL)
  
    for (Day in firstDay:lastDay){
  
      dataframe.sub = subset(dataframe,
                  Date &gt; (Day - (interval/2))
                  &amp; Date &lt; (Day + (interval/2)))
  
      nu = data.frame(Date = Day, 
                      Average = mean(dataframe.sub$Price),
                      Median = median(dataframe.sub$Price),
                      Count = length(dataframe.sub$Price),
                      P25 = quantile(dataframe.sub$Price, 0.25),
                      P75 = quantile(dataframe.sub$Price, 0.75))
  
      result = rbind(result,nu)
  
    }
  
    return(result)
  
  }
  </code></pre>
  
  <p>Your advice would be welcome!</p>