0

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))
dlodge
  • 1
  • 2
  • 3
    Please don't add data as images - it makes it harder for other people to help. Can you edit your question include a sample of your data using `dput()`, and show what code you have already tried? – nrennie Jun 21 '23 at 12:02
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 21 '23 at 14:35
  • @nrennie Sorry, this is my first post on here, but I have updated the question so that it has contains sample data and attempted code. Please let me know if I should provide any other information – dlodge Jun 29 '23 at 11:48

0 Answers0