I have the following following dataset:
data.frame(id=c(1,1,1,1,1,2,2,2,2),
date = as.Date(c("2020-01-01","2020-01-04","2020-01-06","2020-01-07","2020-01-10","2020-01-01","2020-01-02","2020-01-04","2020-01-05")),
duration = c(2,3,4,2,4,3,4,2,2),
product = c("A","B","C","A","C","B","C","A","A"))
I have an id of people, what product they used on each day and also how long will the product last them (duration) - update: products in this sample do have a set duration, but in reality it does not to need to be the case.
I need to make a list of products that are currently in use by each person for each row, so the resulting dataset should look as such (separator is "|" here, but does not matter):
data.frame(id=c(1,1,1,1,1,2,2,2,2),
date = as.Date(c("2020-01-01","2020-01-04","2020-01-06","2020-01-07","2020-01-10","2020-01-01","2020-01-02","2020-01-04","2020-01-05")),
duration = c(2,3,4,2,4,3,4,2,2),
product = c("A","B","C","A","C","B","C","A","A"),
products_in_use = c("A","B","B | C", "A | B | C", "C", "B", "B | C", "A | B | C", "A | C"))
Basically I am thinking I would need to take all rows that are within duration days (as in less or equal days) from the current row and append to their list the current product. Then I would take a unique and ordered version of the list and write is as a string. But I dont know how to do the first step.
It would be preferred if all this would work inside of an dplyr pipe.