I have two date variable one is question open date and another is question answered date, like follows:
q_open q_answer
2020-09-01 2020-09-02
2020-09-01 2020-09-09
2020-09-05 NA
2020-09-10 2020-09-17
I need to find out the counts of the question that are not answered within a week. The week starts from the min(q_open). So week one starts from 2020-09-01 and ends on 2020-09-07. So the desired output is:
Week count_answered
1 2 # considering NA also
2 1
Thanks in advance.
How about this:
library(dplyr)
library(lubridate)
library(tibble)
data <- tibble(
q_open = as.Date(c("2020-09-01", "2020-09-01", "2020-09-05", "2020-09-10")),
q_answer = as.Date(c("2020-09-02", "2020-09-09", NA, "2020-09-17"))
)
out <- data %>%
mutate(wk_open = week(q_open),
wk_answer = week(q_answer)) %>%
mutate(on_time = ifelse(wk_answer >= wk_open + 1, TRUE, FALSE )) %>%
group_by(wk_open) %>%
summarise(count_answered = sum(on_time, na.rm = TRUE))
with:
> out
# A tibble: 3 x 2
wk_open count_answered
<dbl> <int>
1 35 2
2 36 0
3 37 1