I have a simple problem which I have only been able to find unattractive solutions to.
I have time series data with days as the unit of analysis. On certain days, an event takes place. I want to create a variable which indicates all dates following one week of the event, and another variable indicating how many days have passed since the event within this week. I am including an example of what I want to achieve below.
date event week_following_event days_since_event
1 2000-01-01 0 0 NA
2 2000-01-02 0 0 NA
3 2000-01-03 1 0 NA
4 2000-01-04 0 1 1
5 2000-01-05 0 1 2
6 2000-01-06 0 1 3
7 2000-01-07 0 1 4
8 2000-01-08 0 1 5
9 2000-01-09 0 1 6
10 2000-01-10 0 1 7
11 2000-01-11 0 0 NA
12 2000-01-12 0 0 NA
13 2000-01-13 0 0 NA
14 2000-01-14 0 0 NA
15 2000-01-15 0 0 NA
I am pretty sure I could do this by writing a loop, but I am ideally looking for a neater solution.
Here is the dput() output for reproduction purposes:
structure(list(date = structure(c(10957, 10958, 10959, 10960,
10961, 10962, 10963, 10964, 10965, 10966, 10967, 10968, 10969,
10970, 10971), class = "Date"), event = c(0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), week_following_event = c(0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0), days_since_event = c(NA, NA,
NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L, NA, NA, NA, NA, NA)), row.names = c(NA,
-15L), class = "data.frame")
Any input is much appreciated!