I have a question that combines these two questions in some way (Substract date from previous row by group (using R) and subtract value from previous row by group). But cannot manage to get script to work.
I have a dataset that looks something like this:
id | date | min
1 | 2015-07-18 | 25
1 | 2015-07-22 | 15
1 | 2015-07-23 | 10
1 | 2015-07-30 | 15
2 | 2015-07-10 | 10
2 | 2015-07-16 | 20
2 | 2015-07-23 | 10
And I want to create a new column totmin that adds the total number of minutes played in the last 7 days by id:
id | date | min | totmin
1 | 2015-07-18 | 25 | 25
1 | 2015-07-22 | 15 | 40
1 | 2015-07-23 | 10 | 50
1 | 2015-07-30 | 15 | 25
2 | 2015-07-10 | 10 | 10
2 | 2015-07-16 | 20 | 30
2 | 2015-07-23 | 10 | 30
I tried with lag
but do not how to restrict to only 7 days.