'Shifting a date-based analysis by a year: process questions
I am doing a migration study indexed to a specific event. To create the dataset, I basically subset a larger dataset to a specific date, and then made flags based on additional dates, then added in information. In total, this takes 7 scripts. Now, I want to create a comparison dataset, with the same information but indexed to two years earlier.
My question is, is there an easy way where I can use the same script and just tell R to somehow treat all the code as two years before, or do I have to create a duplicate of the code and then edit it in line to be two years before. Here's a very basic example of some of the code I'm using to generate the dataset from a larger framework:
#example of things I'd want shifted 2 years
df <- subset(df, DATE_AFTER > as.Date("2016-09-27"))
df$flag <- with(df,
as.numeric(DATE_BEFORE < as.Date("2016-09-28") &
DATE_AFTER > as.Date("2016-09-27")))
df
# ID DATE_BEFORE DATE_AFTER flag
# 1 A 2013-01-23 2018-01-23 1
# 3 C 2018-01-23 2020-01-23 0
# 5 E 2011-01-23 2019-01-23 1
# 6 F 2010-01-23 2019-01-23 1
# 7 G 2017-01-23 2018-01-23 0
Dummy data
df <- data.frame(ID=c("A", "B", "C", "D", "E", "F", "G"),
DATE_BEFORE=as.Date(c("2013-01-23", "2010-01-23", "2018-01-23",
"2014-01-23", "2011-01-23", "2010-01-23",
"2017-01-23")),
DATE_AFTER=as.Date(c("2018-01-23", "2016-01-23", "2020-01-23",
"2016-01-23", "2019-01-23", "2019-01-23",
"2018-01-23")))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
