Reviewed:
R: cumulative sum over rolling date range
Consecutive/Rolling sums in a vector in R
https://cran.r-project.org/web/packages/RcppRoll/RcppRoll.pdf
There are clearly several options for using rollapply over a range of columns.
I have a dataframe with customer data grouped by month, sample:
year_month customer_id revenue
2018-01-01 1821148 0.00
2018-01-01 142163579 0.00
2018-01-01 16295983 0.00
2018-02-01 1821148 86.57
2018-02-01 142163579 191.21
2018-02-01 16295983 0.00
2018-03-01 1821148 98.18
2018-03-01 142163579 47.61
2018-03-01 16295983 241.88
My question is, how can I use rollapply (e.g. RcppRoll::roll_sum()
or any similar function) for each customer only? Even if I order my data by customer_id and year_month, rollapply won't know to go back e.g 3 months rolling just for a specific customer.
E.g. the 3rd last observation is for customer 1821148 in March. In this case I would like the cumsum for this specific customer in Jan:March.
Even If I order by customer id then year_month, the first observation for a customer will sum the previous 3 rows if they are for another customer.
Is there a way to rollsum for a specific grouping, in this case customer_id?