I have this data frame:
| transaction ID | day number | Predicted value |
|---|---|---|
| 12 | 1 | .001 |
| 12 | 2 | .002 |
| 12 | 1 | .001 |
| 12 | 2 | .002 |
| 13 | 1 | .001 |
| 13 | 2 | .002 |
| 13 | 3 | .002 |
| 13 | 4 | .003 |
I want to take the cumulative sum of the each set of predicted values based on the sequential day numbers (i.e. cumsum of the first 2 rows, cumsum of the next 2, and the cumsum of the last 4)
so the results would be .003, .003, .008
>Solution :
Using R base
sapply(split(df$Predicted_value,cumsum(c(1,diff(df$day_number)!=1))), sum)
1 2 3
0.003 0.003 0.008