title: Computing a weighted rolling average R
body: <p>Say i have two columns in a dataframe/data.table, one the level and the other one volume. I want to compute a rolling average of the level, weighted by volume, so volume acts as weight (normalized to 1) for some rolling window.</p>
  
  <p>Base R has a weighted.mean() function which does similar calculation for two static vectors. I tried using sapply to pass a list/vector fo argument to it and create a rollign series, but to no avail.</p>
  
  <p>Which "apply" mechanism should i use with weighted.mean() to get the desired result, or i would have to loop/write my own function?</p>
  
  <p>////////////////////////////////////////////////////////////////////////////////////////</p>
  
  <p>P.S. in the end i settled on writing simple custom function, which utilizes the great RccpRoll package. I found RccpRoll to be wicked fast, much faster than other rolling methods, which is important to me, as my data is several million rows.</p>
  
  <p>the code for the function looks like this(i've added some NAs in the beggining since RccpRoll returns data without NAs):</p>
  
  <pre><code>require(RcppRoll)
  my.rollmean.weighted &lt;- function(vec1,vec2,width){
     return(c(rep(NA,width-1),roll_sum(vec1*vec2,width)/roll_sum(vec2,width)))
  }
  </code></pre>