I have a tibble structured as follows:
day theta
1 1 2.1
2 1 2.1
3 2 3.2
4 2 3.2
5 5 9.5
6 5 9.5
7 5 9.5
Note that the tibble contains multiple rows for each day
, and for each day
the same value for theta
is repeated an arbitrary number of times. (The tibble contains other arbitrary columns necessitating this repeating structure.)
I'd like to use dplyr
to cumulatively sum values for theta
across days such that, in the example above, 2.1
is added only a single time to 3.2
, etc. The tibble would be mutated so as to append the new cumulative sum (c.theta
) as follows:
day theta c.theta
1 1 2.1 2.1
2 1 2.1 2.1
3 2 3.2 5.3
4 2 3.2 5.3
5 5 9.5 14.8
6 5 9.5 14.8
7 5 9.5 14.8
...
My initial efforts to group_by
day
and then cumsum
over theta
resulted only in cumulative summing over the full set of data (e.g., 2.1 + 2.1 + 3.2 ...
) which is undesirable. In my Stack Overflow searches, I can find many examples of cumulative summing within groups, but never between groups, as I describe above. Nudges in the right direction would be much appreciated.