I am trying to figure out how to make multiple customer orders placed within 120 days wide format for each customer. If a customer has orders placed over the course of several year, then there are multiple intervals of 120 days, so each customer may have multiple rows, with each row corresponding to an interval of 120 days from an index date. The index date(s) would be the first order date that falls outside the 120-day interval from the prior index order date. Thank you!
I have a dataframe:
customerid <- c("A1", "A1", "A1", "A1", "A1", "A2", "A2", "A2")
orderid <- c("1", "2", "3", "4", "5", "6", "7", "8")
orderdate <- c("2020-05-19", "2020-09-08", "2020-09-16", "2020-12-21", "2021-01-03", "2020-08-21","2020-11-22","2021-02-01")
df <- data.frame(customerid, orderid, orderdate)
The result should be:
Thank you!