title: Rolling sum of time series with factor
body: <p>I am trying to calculate a rolling sum for a time series of returns <code>r</code> ranging over T dates. However at each date t when I calculate the rolling sum, I want to factor in a weight <code>w</code> for each number in the rolling sum.</p>
  
  <p>The formula would be for every date t:</p>
  
  <pre><code>[Sum from i=1 to m](w(i)*r(t-i-1))
  </code></pre>
  
  <p>Lets look at a very simple example. I have a return series of T=6 returns <code>r</code>. For each date <code>t</code> I want to calculate the rolling sum over the last two dates (m=2). I also want to weight the first observation twice as much as the second.</p>
  
  <pre><code>r &lt;- c(100,110,100,110,100,110)                 
  w &lt;- c(1,0.5)
  </code></pre>
  
  <p>I know that I can easily do the rolling sum using the filter function:</p>
  
  <pre><code>filter(r, rep(1, 2))
  </code></pre>
  
  <p>However I am not able to include the weight factor into the rolling sum. The following line gives the wrong result of <code>c(155, 155, 155, 155, 155,  NA)</code></p>
  
  <pre><code>filter(r*w, rep(1, 2))
  </code></pre>
  
  <p>where I would really like to have the result <code>c(155, 160, 155, 160, 155, NA)</code></p>
  
  <p>Any help is appreciated. </p>