title: dynamic correlation between vectors (with sampling)
body: <p>Say, I have two vectors of the same length</p>
  
  <pre><code>A = mtcars$mpg
  B = mtcars$cyl
  </code></pre>
  
  <p>I can calculate correlation between whole vectors</p>
  
  <pre><code>cor (A, B)
  </code></pre>
  
  <p>and get one single value (<code>-0.852162</code>).</p>
  
  <p>What I need is to calculate correlation between the two vectors with a sampling rate of 10, which means I start at the first datapoint in A and B, take 5 values on the right from it (there are no values on the left), calculate a correlation coefficient ad write it in a vector C. Then I take the next value in A &amp; B, take 5 values on the right and 1 on the left, write it into a vector; then shift again to the next value and so forth. The resulting vector C must contain the same number of values as A or B (N=32), and each value in C represents a correlation b/w A and B with a sampling rate 10 (5 values on the left and 5 on the right from that datapoint, if availible).
  Is there any elegant and simple way to do it in R?</p>
  
  <p>P.S.: The ease of coding is more important, than the time needed for calculations.</p>