Say that I have a dataset.
date <- c("2004-02-01", "2004-03-05", "2004-08-09", "2004-08-13", "2004-10-20", "2004-11-02", "2008-01-05", "2008-02-03", "2008-08-09", "2008-11-04", "2012-01-05", "2012-02-03", "2012-08-09", "2012-10-04", "2012-10-04", "2012-10-31", "2012-11-04")
date <- ymd(date)
name <- c("Joe", "Joe", "Joe", "Joe", "Joe", "Joe",
"Larry", "Larry", "Larry", "Larry",
"Jeff", "Jeff", "Jeff", "Jeff", "Jeff", "Jeff", "Jeff")
hits <- c(5, 4, 10, 9, 15, 1,
13, 22, 9, 11,
15, 17, 10, 3, 4, 2, 33)
df <- data.frame(date, name, hits)
I want to do 7-day time lags for each observation. In order to do this, I will have to restructure the dataset a bit.
I want to add seven days after each date for each name, but the hits will be 0. I hope to end up with a dataset like the following (for Joe):
date name hits
2004-02-01 Joe 5
2004-02-02 Joe 0
2004-02-03 Joe 0
2004-02-04 Joe 0
2004-02-05 Joe 0
2004-02-06 Joe 0
2004-02-07 Joe 0
2004-02-08 Joe 0
2004-03-05 Joe 4
2004-03-06 Joe 0
2004-03-07 Joe 0
2004-03-08 Joe 0
2004-03-09 Joe 0
2004-03-10 Joe 0
2004-03-11 Joe 0
2004-03-12 Joe 0
2004-08-09 Joe 10
2004-08-10 Joe 0
2004-08-11 Joe 0
2004-08-12 Joe 0
2004-08-13 Joe 9
2004-08-14 Joe 0
2004-08-15 Joe 0
2004-08-16 Joe 0
2004-08-17 Joe 0
2004-08-18 Joe 0
2004-08-19 Joe 0
2004-08-20 Joe 0
2004-10-20 Joe 15
2004-10-21 Joe 0
2004-10-22 Joe 0
2004-10-23 Joe 0
2004-10-24 Joe 0
2004-10-25 Joe 0
2004-10-26 Joe 0
2004-10-27 Joe 0
2004-11-02 Joe 1
2004-11-03 Joe 0
2004-11-04 Joe 0
2004-11-05 Joe 0
2004-11-06 Joe 0
2004-11-07 Joe 0
2004-11-08 Joe 0
2004-11-09 Joe 0
Is there a fast way to do this using dplyr?