title: Creating a running counting variable in R?
body: <p>I have a dataset of soccer match results, and I am hoping to learn R by creating a running set of ratings similar to the World Football Elo formula.  I am running into trouble with things that seem to be simple in Excel aren't exactly intuitive in R. For instance, the first 15 of 4270 observations with the necessary variables:</p>
  <pre class="lang-none prettyprint-override"><code>       date t.1  t.2 m.result
  1  19960406  DC   SJ      0.0
  2  19960413 COL   KC      0.0
  3  19960413  NE   TB      0.0
  4  19960413 CLB   DC      1.0
  5  19960413 LAG NYRB      1.0
  6  19960414 FCD   SJ      0.5
  7  19960418 FCD   KC      1.0
  8  19960420  NE NYRB      1.0
  9  19960420  DC  LAG      0.0
  10 19960420 CLB   TB      0.0
  11 19960421 COL  FCD      1.0
  12 19960421  SJ   KC      0.5
  13 19960427 CLB NYRB      1.0
  14 19960427  DC   NE      0.5
  15 19960428 FCD   TB      1.0
  </code></pre>
  <p>I want to be able to create a new variable that will be a running count of t.1 and t.2's total matches played (i.e., the instances up to the date in question that &quot;DC&quot; occurs in columns t.1 or t.2):</p>
  <pre class="lang-none prettyprint-override"><code>           date t.1  t.2 m.result  ##t.1m    ##t.2m
      1  19960406  DC   SJ      0.0       1         1
      2  19960413 COL   KC      0.0       1         1
      3  19960413  NE   TB      0.0       1         1
      4  19960413 CLB   DC      1.0       1         2
      5  19960413 LAG NYRB      1.0       1         1
      6  19960414 FCD   SJ      0.5       1         2
      7  19960418 FCD   KC      1.0       2         2
      8  19960420  NE NYRB      1.0       2         2
      9  19960420  DC  LAG      0.0       3         2
      10 19960420 CLB   TB      0.0       2         2
      11 19960421 COL  FCD      1.0       2         3
      12 19960421  SJ   KC      0.5       3         3
      13 19960427 CLB NYRB      1.0       3         3
      14 19960427  DC   NE      0.5       4         3
      15 19960428 FCD   TB      1.0       4         3
  </code></pre>
  <p>in Excel, this is a (relatively) simple =SUMPRODUCT equation, e.g:</p>
  <pre><code>E4=SUMPRODUCT((A:A&lt;=A4)*(B:B=B4))+SUMPRODUCT((A:A&lt;=A4)*(C:C=B4))
  </code></pre>
  <p>where E4 is t.1m for obs # 4, A:A is Date, B:B is t.1, C:C is t.2, etc.</p>
  <p>But in R, I can get total sumproduct printed for me (i.e. &quot;DC&quot; has played 576 games across my dataset), but for some reason (probably that I'm new, impatient, rattled by trial and error) I'm just lost on how to make a running count on observation data, and especially how to make that running count into a variable, which is vital for any game rating index.  I know 'PlayerRatings' exists, I feel that for my R education I should be able do this in the R suite without that package. plyr or dplyr is okay, of course.</p>
  <p>For reference, here is my data for you to copy/paste into your R.</p>
  <pre class="lang-r prettyprint-override"><code>date&lt;-c(19960406,19960413,19960413,19960413,19960413,19960414,19960418,19960420,19960420,19960420,19960421,19960421,19960427,19960427,19960428)
  t.1&lt;-c(&quot;DC&quot;,&quot;COL&quot;,&quot;NE&quot;,&quot;CLB&quot;,&quot;LAG&quot;,&quot;FCD&quot;,&quot;FCD&quot;,&quot;NE&quot;,&quot;DC&quot;,&quot;CLB&quot;,&quot;COL&quot;,&quot;SJ&quot;,&quot;CLB&quot;,&quot;DC&quot;,&quot;FCD&quot;)
  t.2&lt;-c(&quot;SJ&quot;,&quot;KC&quot;,&quot;TB&quot;,&quot;DC&quot;,&quot;NYRB&quot;,&quot;SJ&quot;,&quot;KC&quot;,&quot;NYRB&quot;,&quot;LAG&quot;,&quot;TB&quot;,&quot;FCD&quot;,&quot;KC&quot;,&quot;NYRB&quot;,&quot;NE&quot;,&quot;TB&quot;)
  m.result&lt;-c(0.0,0.0,0.0,1.0,1.0,0.5,1.0,1.0,0.0,0.0,1.0,0.5,1.0,0.5,1.0)
  mtable&lt;-data.frame(date,t.1,t.2,m.result)
  mtable
  </code></pre>