title: R - cumulative count over multiple columns, by factor
body: <p>I have a dataset which simplified look something like this:</p>
  
  <pre><code>YEAR = c(2009,2009,2009,2009,2009,2009,2009,2010,2010,2010,2010,2010,2010,2010)
  FROM = c("A","C","B","D","B","A","C","A","C","B","A","D","B","A")
  TO = c("B","D","C","A","D","C","B","B","A","D","D","C","A","D")
  DATA = data.frame(YEAR,FROM,TO)
  
  YEAR    FROM    TO
  2009    A       B
  2009    C       D
  2009    B       C
  2009    D       A
  2009    B       D
  2009    A       C
  2009    C       B
  2010    A       B
  2010    C       A
  2010    B       D
  2010    A       D
  2010    D       C
  2010    B       A
  2010    A       D
  </code></pre>
  
  <p>What I want is two additional columns, let's say OCC_FROM and OCC_TO, that is the cumulative count of occurrences in both FROM and TO columns in prior rows, by YEAR. Like this:</p>
  
  <pre><code>YEAR    FROM    TO  OCC_FROM    OCC_TO
  2009    A       B   0           0
  2009    C       D   0           0
  2009    B       C   1           1
  2009    C       A   2           1
  2009    B       D   2           1
  2009    A       C   2           3
  2009    C       B   4           3
  2010    A       B   0           0
  2010    C       A   0           1
  2010    B       D   1           0
  2010    A       B   2           2
  2010    D       C   1           1
  2010    B       A   3           3
  2010    A       D   4           2
  </code></pre>
  
  <p>What I've managed to produce, with the help of <a href="https://stackoverflow.com/questions/10029235/cumulative-count-in-r">Cumulative count in R</a>, is this, which is obviously not quite what I want since it doesn't take YEAR into consideration:</p>
  
  <pre><code>DATA$OCC_FROM = sapply(1:length(DATA$FROM),function(i)sum(DATA$FROM[i]==DATA$FROM[1:i]))+sapply(1:length(DATA$FROM),function(i)sum(DATA$FROM[i]==DATA$TO[1:i]))-1
  DATA$OCC_TO = sapply(1:length(DATA$TO),function(i)sum(DATA$TO[i]==DATA$FROM[1:i]))+sapply(1:length(DATA$TO),function(i)sum(DATA$TO[i]==DATA$TO[1:i]))-1
  
  YEAR    FROM    TO  OCC_FROM    OCC_TO
  2009    A       B   0           0
  2009    C       D   0           0
  2009    B       C   1           1
  2009    C       A   2           1
  2009    B       D   2           1
  2009    A       C   2           3
  2009    C       B   4           3
  2010    A       B   3           4
  2010    C       A   5           4
  2010    B       D   5           2
  2010    A       B   5           6
  2010    D       C   3           6
  2010    B       A   7           6
  2010    A       D   7           4
  </code></pre>
  
  <p>Edit: I also want to be able to sum two columns cumulatively based on FROM and TO, by YEAR as before. For simplicity I'll use OCC_FROM and OCC_TO. Like this:</p>
  
  <pre><code>YEAR    FROM    TO  OCC_FROM    OCC_TO    TOTAL_FROM    TOTAL_TO
  2009    A       B   0           0         0             0
  2009    C       D   0           0         0             0
  2009    B       C   1           1         0             0
  2009    C       A   2           1         1             0
  2009    B       D   2           1         1             0
  2009    A       C   2           3         1             3
  2009    C       B   4           3         6             3
  2010    A       B   0           0         0             0
  2010    C       A   0           1         0             0
  2010    B       D   1           0         0             0
  2010    A       B   2           2         1             1
  2010    D       C   1           1         0             0
  2010    B       A   3           3         3             3
  2010    A       D   4           2         6             1
  </code></pre>