title: Cumulative sum conditional over multiple columns in r dataframe containing the same values
body: <p>Say my data.frame is as outlined below:</p>
  
  <pre><code>df&lt;-as.data.frame(cbind("Home"=c("a","c","e","b","e","b"),
  "Away"=c("b","d","f","c","a","f"))
  df$Index&lt;-rep(1,nrow(df))
  
     Home Away Index
  1    a    b     1
  2    c    d     1
  3    e    f     1
  4    b    c     1
  5    e    a     1
  6    b    f     1
  </code></pre>
  
  <p>What I want to do is calculate a cumulative sum using the Index column for each character a - f regardless of whether they in the <code>Home</code> or <code>Away</code> columns. Thus a column called <code>Cumulative_Sum_Home</code>, say, takes the character in the <code>Home</code> row, "b" in the case of row 6, and counts how many times "b" has appeared in either the <code>Home</code> or <code>Away</code> columns in all previous rows including row 6. Thus in this case b has appeared 3 times cumulatively in the first 6 rows, and thus the <code>Cumulative_Sum_Home</code> gives the value 3. Likewise the same logic applies to the <code>Cumulative_Sum_Away</code> column. Taking row 5, character "a" appears in the Away column, and has cumulatively appeared 2 times in either Home or Away columns up to that row, so the column <code>Cumulative_Sum_Away takes</code> the value 2.</p>
  
  <pre><code> Home Away Index   Cumulative_Sum_Home  Cumulative_Sum_Away
  1    a    b     1        1                    1
  2    c    d     1        1                    1
  3    e    f     1        1                    1
  4    b    c     1        2                    2 
  5    e    a     1        2                    2 
  6    b    f     1        3                    2 
  </code></pre>
  
  <p>I have to confess to being totally stumped as to how to solve this problem. I've tried looking at the <code>data.table</code> approaches, but I've never used that package before so I can't immediately see how to solve it. Any tips would be greatly received. </p>