title: R Moving-average per group
body: <p>I got a data.frame <code>ABC_Score</code>. </p>
  
  <pre><code>ABC_Score &lt;- data.frame(Level = c("A", "A", "A", "B", "B", "C", "C", 
  "C", "C"), result = c(2, 3, 3, 7, 9, 18, 20, 17, 20))
  </code></pre>
  
  <p>What I need is the moving average of the <code>result</code> per <code>Level</code>. </p>
  
  <p>Currently I have the moving average with the following script. </p>
  
  <pre><code>install.packages("TTR")
  library(TTR)
  
  `ABC_Score$MA` &lt;- runMean(ABC_Score$result, 2)
  
        Level result   MA
      1     A      2   NA
      2     A      3  2.5
      3     A      3  3.0
      4     B      7  5.0
      5     B      9  8.0
      6     C     18 13.5
      7     C     20 19.0
      8     C     17 18.5
      9     C     20 18.5
  </code></pre>
  
  <p>but here I need to specify the number of rows (<code>result</code>) where the moving average will be calculated over. In this example <code>2</code> which is incorrect since <code>results</code> from different <code>Levels</code> are now mixed in the moving average.</p>
  
  <p>How can the moving average automatically be calculated over the <code>result</code> per <code>Level</code>?</p>