title: how to calculate "consecutive mean" in R without using loop, or in a more efficient way?
body: <p>I have a set a data that I need to calculate their "consecutive mean" (I dunno if it is the correct name, but I can't find anything better), here is an example:</p>
  
  <pre><code>ID  Var2 Var3    
  1    A    1
  2    A    3
  3    A    5
  4    A    7
  5    A    9
  6    A    11
  7    B    2
  8    B    4
  9    B    6
  10   B    8
  11   B    10
  </code></pre>
  
  <p>Here I need to calculated the mean of 3 Var3 variable in the same subset consecutively (i.e. there will be 4 means caulculated for A: mean(1,3,5), mean(3,5,7), mean(5,7,9), mean(7,9,11), and 3 means calculated for B: mean(2,4,6), mean(4,6,8), mean(6,8,10). And the result should be:</p>
  
  <pre><code>ID  Var2 Var3 Mean
  1    A    1   N/A
  2    A    3   N/A
  3    A    5   3
  4    A    7   5
  5    A    9   7
  6    A    11  9
  7    B    2   N/A
  8    B    4   N/A
  9    B    6   4
  10   B    8   6
  11   B    10  8
  </code></pre>
  
  <p>Currently I am using a "loop-inside-a-loop" approach, I subset the dataset using Var2, and then I calculated the mean in another start from the third data.</p>
  
  <p>It suits what I need, but it is very slow, is there any faster way for this problem?</p>
  
  <p>Thanks!</p>