I could not solve this issue even after looking at several answers on stack overflow. I have a dataset with a list of dates and a list of intervals. I need to find how many intervals contains a date, for that given date. I could find several question on how many date were contained in a interval, but this is not what i am looking for.
Here is a reproducible example
d<-data.frame(ID=c(80, 736, 54, 259, 826, 446, 950, 841, 433, 518, 1357,
3686, 4042, 749, 2716, 4568, 1424, 332, 1000, 575, 1815, 3074,
3768, 932, 4, 3872, 2033, 2495, 3310),
date=ymd(c("2022-02-20", "2022-02-21", "2022-02-22", "2022-02-23", "2022-02-24",
"2022-02-25", "2022-02-26", "2022-02-27", "2022-02-28",
"2022-03-01", "2022-03-02", "2022-03-02", "2022-03-03", "2022-03-04",
"2022-03-05", "2022-03-05", "2022-03-06", "2022-03-07", "2022-03-08",
"2022-03-09", "2022-03-10", "2022-03-10", "2022-03-10", "2022-03-11",
"2022-03-12", "2022-03-12", "2022-03-13", "2022-03-13", "2022-03-13")),
start.date= ymd(c( NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "2022-03-02", "2022-03-02",
"2022-03-03", "2022-03-04", "2022-03-05", "2022-03-05", "2022-03-06",
NA, "2022-03-08", "2022-03-09", "2022-03-10", "2022-03-10", "2022-03-10",
NA, "2022-03-12", "2022-03-12", "2022-03-13", "2022-03-13", "2022-03-13")),
end.date=ymd(c( NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "2022-03-15", "2022-03-10",
"2022-03-07", "2022-03-14", "2022-03-29", "2022-03-17", "2022-03-21",
NA, "2022-03-27", "2022-03-16", "2022-03-16", "2022-03-24", "2022-03-18",
NA, "2022-03-22", "2022-03-18", "2022-03-22", "2022-03-30", "2022-03-19"
)))
d<-d %>% mutate(interval=(start.date %--% end.date)) %>% select(-start.date,-end.date)
what I would like to get is, in a new column, for each date, the number of interval containing the date.
I normally use dplyr-lubridate, I tried to solve this with purrr but was not able to succeed.
Any suggestion?
Thank you
EDIT:i tried with a purrr solution like the following
d %>% mutate(dates_in_intv = map_int(interval, function(x) sum(.$date %within% x)))
which is counting over how many dates an interval is spanning, but what I need would be something like
d %>% mutate(intv_contains_dates= map_int(date, function(x) sum(.$interval "contains" x)))