I have a dataset with start and end dates of service use for individuals (one row for each episode). Sometimes these periods overlap, sometimes they don't. I want to count the number of unique days in a year the person has touched a service (using R). Tried to use the IVS package but am running into issues as this struggles with rows where the start and end date are on the same day. How do I count distinct days where the same person has single days of service as well as episodes of multiple days.
eg_data <- data.frame(
id = c(1,1,1, 2,2, 3,3,3,3,3,3, 4,4, 5,5,5,5),
start_dt = c("01/01/2016", "12/02/2016", "03/12/2017", "02/01/2016",
"03/04/2016", "01/01/2016", "03/05/2016", "05/07/2016", "07/01/2016",
"09/04/2016", "10/10/2016", "01/01/2016", "05/28/2016", "01/01/2016",
"06/05/2016", "08/25/2016", "11/01/2016"),
end_dt = c("12/01/2016", "12/02/2016", "05/15/2017", "05/15/2016",
"12/29/2016", "03/02/2016", "04/29/2016", "06/29/2016", "08/31/2016",
"03/04/2016", "11/29/2016", "05/31/2016", "08/19/2016", "06/10/2016",
"07/25/2016", "08/25/2016", "12/30/2016"))
eg_data$row_n <- 1:nrow(eg_data)
Tried
ab <- a %>%
mutate(
start_dt = as.Date(ActivityStartDate, format = "%m/%d/%Y"),
end_dt = as.Date(ActivityEndDate, format = "%m/%d/%Y")
) %>%
mutate(
range = iv(start_dt, end_dt),
.keep = "unused"
)
c <-ab %>%
group_by(ID) %>%
mutate(group = iv_identify_group(range)) %>%
group_by(group, .add = TRUE)
But doesn't work for records where start and end date are on the same day. Also want the output to be a dataframe with date variables, not a vector, so I can calculate the total number of days with activity (without counting the same day more than once).