I want to count the total number of people who were present on a given day. They have an interval (start.date) and (end.date). How can I count all those people who's interval fell within a given date? I thought about using lubridate and interval(), but I am still stuck as how to iterate this for each day? Please help.
first_date = as.Date(min("2011-01-01"))
last_date = as.Date(max("2011-12-31"))
all_dates = seq(first_date, by=1, to=last_date)
CY_2015 <- data.frame(DATE = all_dates)
df <- data.frame(ID = c(1,2,3,4),
START.DATE = as.Date(c("2011-07-24", "2011-07-19", "2011-07-08", "2011-07-23")),
END.DATE = as.Date(c("2011-07-26", "2011-07-21", "2011-07-10", "2011-07-25")))
# I tried something like this to no avail:
for (i in 1:365) {
DATE <- CY_2015$DATE[i]
df <- df %>% mutate(new_var = in.interval.lo(DATE, START.DATE, END.DATE))
names(df)[length(df)] <- paste0(DATE)
}