I am trying to determine when is the best time to call patients and have them answer the phone. Is there a way to find the time of day AND day of week that results in most number of answered calls (i.e. Wednesday from 1-2 pm)? Thanks!
Here is some sample code for reference:
df <- data.frame(time  = c("09:55", "10:10", "10:15", "10:55", 
                 "12:35", "13:45", "15:30", "15:45", "17:00", 
                 "11:15"),
                 date = c("2023-05-19", "2023-05-12", "2023-05- 
                 24", "2023-05-12", "2023-05-24", "2023-05-24", 
                 "2023-05-16", "2023-05-14", "2023-05-16", "2023- 
                  05-18"), 
                 picked_up = c("No", "No", "No", "No", "Yes", 
                 "Yes", "No", "No", "Yes", "Yes")) 
This is the code I used to determine what day of the week, turn "Yes" or "No" for "picked up" into a binary variable (1/0) and organize time into hour bins from 09:00 - 18:00.
df$dow <- as.Date(df$date)
df$dow <- weekdays(df$dow)
df$binary <- if_else(df$picked_up =="Yes", 1, 0)
df$hour <- format(strptime(df$time, "%H:%M"), 
"%H:00")  
This is the code I used to get the frequency tables and
freq_time <- df %>% group_by(hour) %>% summarize(freq = 
sum(binary))
freq_day <- df %>% group_by(dow) %>% summarise(freq =
sum(binary))
I could theoretically find the answer by taking every iteration (see below) but I am basically trying to figure out a simpler way to run every iteration of the code below for Mon-Fri and 09:00 - 18:00 and find the one with the greatest value. I tried max and which.max but was not able to get output I needed.
picked_up <- subset(df, binary == 1) 
nrow(picked_up[picked_up$dow=="Monday" & picked_up$hour == 
"09:00"])
nrow(picked_up[picked_up$dow=="Monday" & picked_up$hour == 
"10:00"])
nrow(picked_up[picked_up$dow=="Monday" & picked_up$hour == 
"11:00"])
nrow(picked_up[picked_up$dow=="Monday" & picked_up$hour == 
"12:00"]))
I also tried this code based on a similar post, but I am getting nearly identical graphs for each day of the week, so unsure why this is.
df %>%
group_by(dow) %>%
group_by(hour) %>%
mutate(resp = sum(binary)) %>%
ungroup() %>%
ggplot(aes(x=hour, y=resp)) +
geom_point()+
ylab("Number of Responses") + 
xlab("Hour") +
facet_wrap(vars(dow))
