Building on Check if a date is within an interval in R, we want to see if a specific event falls into a timeframe specified by another event. To give you a concrete example: For each country, did event (battle/protests/...) happen at the time of elections?
country <- c("Angola","Angola","Angola","Angola","Angola", "Benin","Benin","Benin","Benin","Benin","Benin")
event_type <- c("battle", "protests","riots", "riots", "elections","elections","protests","riots","violence","riots","elections")
event_date <- as.Date(c("2017-06-16", "2017-01-23", "2016-03-15", "2017-09-18", "2017-08-23", "2019-04-18", "2019-03-12", "2019-04-14", "2018-03-15", "2015-09-14", "2016-03-20"))
start_ecycle <- as.Date(c(NA,NA,NA,NA,"2017-05-25", "2019-01-18",NA,NA,NA,NA,"2015-12-21"))
end_ecycle <-as.Date(c(NA,NA,NA,NA,"2017-09-22","2019-05-18",NA,NA,NA,NA,"2016-04-19"))
mydata <- data.frame(country, event_type, event_date, start_ecycle, end_ecycle)
To this end, we created an interval variable
library(lubridate)
is.instant(mydata$start_ecycle); is.instant(mydata$end_ecycle)
mydata$ecycle <- interval(mydata$start_ecycle, mydata$end_ecycle)
Now, we got stuck. This is what the data.frame should look like in the end - i.e. here column G "ecycle_within" is added with 1 if event_date falls within ecycle (per country):
Any help much appreciated. Thanks!