title: simple examples of filter function, recursive option specifically
body: <p>I am seeking some simple (i.e. - no maths notation, long-form reproducible code) examples for the <code>filter</code> function in R
  I think I have my head around the convolution method, but am stuck at generalising the recursive option. I have read and battled with various documentation, but the help is just a bit opaque to me.</p>
  
  <p>Here are the examples I have figured out so far:</p>
  
  <pre><code># Set some values for filter components
  f1 &lt;- 1; f2 &lt;- 1; f3 &lt;- 1;
  </code></pre>
  
  <p>And on we go:</p>
  
  <pre><code># basic convolution filter
  filter(1:5,f1,method="convolution")
  [1] 1 2 3 4 5
  
  #equivalent to:
  x[1] * f1 
  x[2] * f1 
  x[3] * f1 
  x[4] * f1 
  x[5] * f1 
  
  # convolution with 2 coefficients in filter
  filter(1:5,c(f1,f2),method="convolution")
  [1]  3  5  7  9 NA
  
  #equivalent to:
  x[1] * f2 + x[2] * f1
  x[2] * f2 + x[3] * f1
  x[3] * f2 + x[4] * f1 
  x[4] * f2 + x[5] * f1 
  x[5] * f2 + x[6] * f1
  
  # convolution with 3 coefficients in filter
  filter(1:5,c(f1,f2,f3),method="convolution")
  [1] NA  6  9 12 NA
  
  #equivalent to:
   NA  * f3 + x[1] * f2 + x[2] * f1  #x[0] = doesn't exist/NA
  x[1] * f3 + x[2] * f2 + x[3] * f1
  x[2] * f3 + x[3] * f2 + x[4] * f1 
  x[3] * f3 + x[4] * f2 + x[5] * f1 
  x[4] * f3 + x[5] * f2 + x[6] * f1
  </code></pre>
  
  <p>Now's when I am hurting my poor little brain stem.
  I managed to figure out the most basic example using info at this post: <a href="https://stackoverflow.com/a/11552765/496803">https://stackoverflow.com/a/11552765/496803</a></p>
  
  <pre><code>filter(1:5, f1, method="recursive")
  [1]  1  3  6 10 15
  
  #equivalent to:
  
  x[1]
  x[2] + f1*x[1]
  x[3] + f1*x[2] + f1^2*x[1]
  x[4] + f1*x[3] + f1^2*x[2] + f1^3*x[1]
  x[5] + f1*x[4] + f1^2*x[3] + f1^3*x[2] + f1^4*x[1]
  </code></pre>
  
  <p>Can someone provide similar code to what I have above for the convolution examples for the recursive version with <code>filter = c(f1,f2)</code> and <code>filter = c(f1,f2,f3)</code>?</p>
  
  <p>Answers should match the results from the function:</p>
  
  <pre><code>filter(1:5, c(f1,f2), method="recursive")
  [1]  1  3  7 14 26
  
  filter(1:5, c(f1,f2,f3), method="recursive")
  [1]  1  3  7 15 30
  </code></pre>
  
  <h1>EDIT</h1>
  
  <p>To finalise using @agstudy's neat answer:</p>
  
  <pre><code>&gt; filter(1:5, f1, method="recursive")
  Time Series:
  Start = 1 
  End = 5 
  Frequency = 1 
  [1]  1  3  6 10 15
  &gt; y1 &lt;- x[1]                                            
  &gt; y2 &lt;- x[2] + f1*y1      
  &gt; y3 &lt;- x[3] + f1*y2 
  &gt; y4 &lt;- x[4] + f1*y3 
  &gt; y5 &lt;- x[5] + f1*y4 
  &gt; c(y1,y2,y3,y4,y5)
  [1]  1  3  6 10 15
  </code></pre>
  
  <p>and...</p>
  
  <pre><code>&gt; filter(1:5, c(f1,f2), method="recursive")
  Time Series:
  Start = 1 
  End = 5 
  Frequency = 1 
  [1]  1  3  7 14 26
  &gt; y1 &lt;- x[1]                                            
  &gt; y2 &lt;- x[2] + f1*y1      
  &gt; y3 &lt;- x[3] + f1*y2 + f2*y1
  &gt; y4 &lt;- x[4] + f1*y3 + f2*y2
  &gt; y5 &lt;- x[5] + f1*y4 + f2*y3
  &gt; c(y1,y2,y3,y4,y5)
  [1]  1  3  7 14 26
  </code></pre>
  
  <p>and...</p>
  
  <pre><code>&gt; filter(1:5, c(f1,f2,f3), method="recursive")
  Time Series:
  Start = 1 
  End = 5 
  Frequency = 1 
  [1]  1  3  7 15 30
  &gt; y1 &lt;- x[1]                                            
  &gt; y2 &lt;- x[2] + f1*y1      
  &gt; y3 &lt;- x[3] + f1*y2 + f2*y1
  &gt; y4 &lt;- x[4] + f1*y3 + f2*y2 + f3*y1
  &gt; y5 &lt;- x[5] + f1*y4 + f2*y3 + f3*y2
  &gt; c(y1,y2,y3,y4,y5)
  [1]  1  3  7 15 30
  </code></pre>