I have the following data:
data <- tibble::tribble(
NA~NA, ~Transaction, ~Date, ~transTime,
NA1, 536365, "12/1/10 8:26", "8:26:00",
NA2, 536366, "12/1/10 8:28", "8:28:00",
NA3, 536367, "12/1/10 8:34", "8:34:00",
NA4, 536368, "12/1/10 8:34", "8:34:00",
NA5, 536369, "12/1/10 8:35", "8:35:00",
NA6, 536370, "12/1/10 8:45", "8:45:00",
NA7, 536371, "12/1/10 9:00", "9:00:00",
NA8, 536372, "12/1/10 9:01", "9:01:00",
NA9, 536373, "12/1/10 9:02", "9:02:00",
NA10, 536374, "12/1/10 9:09", "9:09:00",
NA11, 536375, "12/1/10 9:32", "9:32:00",
NA12, 536376, "12/1/10 9:32", "9:32:00",
NA13, 536377, "12/1/10 9:34", "9:34:00",
NA14, 536378, "12/1/10 9:37", "9:37:00",
NA16, 536380, "12/1/10 9:41", "9:41:00",
NA17, 536381, "12/1/10 9:41", "9:41:00",
NA18, 536382, "12/1/10 9:45", "9:45:00",
NA20, 536384, "12/1/10 9:53", "9:53:00",
NA21, 536385, "12/1/10 9:56", "9:56:00",
NA22, 536386, "12/1/10 9:57", "9:57:00",
NA23, 536387, "12/1/10 9:58", "9:58:00",
NA24, 536388, "12/1/10 9:59", "9:59:00",
NA25, 536389, "12/1/10 10:03", "10:03:00",
NA26, 536390, "12/1/10 10:19", "10:19:00",
NA28, 536392, "12/1/10 10:29", "10:29:00",
NA29, 536393, "12/1/10 10:37", "10:37:00",
NA30, 536394, "12/1/10 10:39", "10:39:00",
NA31, 536395, "12/1/10 10:47", "10:47:00",
NA32, 536396, "12/1/10 10:51", "10:51:00",
NA33, 536397, "12/1/10 10:51", "10:51:00",
NA34, 536398, "12/1/10 10:52", "10:52:00",
NA35, 536399, "12/1/10 10:52", "10:52:00",
NA36, 536400, "12/1/10 10:53", "10:53:00",
NA37, 536401, "12/1/10 11:21", "11:21:00",
NA38, 536402, "12/1/10 11:22", "11:22:00",
NA39, 536403, "12/1/10 11:27", "11:27:00",
NA40, 536404, "12/1/10 11:29", "11:29:00",
NA41, 536405, "12/1/10 11:32", "11:32:00",
NA42, 536406, "12/1/10 11:33", "11:33:00",
NA43, 536407, "12/1/10 11:34", "11:34:00",
NA44, 536408, "12/1/10 11:41", "11:41:00",
NA45, 536409, "12/1/10 11:45", "11:45:00",
NA46, 536412, "12/1/10 11:49", "11:49:00",
NA47, 536414, "12/1/10 11:52", "11:52:00",
NA48, 536415, "12/1/10 11:57", "11:57:00",
NA49, 536416, "12/1/10 11:58", "11:58:00",
NA50, 536420, "12/1/10 12:03", "12:03:00",
NA51, 536423, "12/1/10 12:08", "12:08:00",
NA52, 536425, "12/1/10 12:08", "12:08:00",
NA53, 536437, "12/1/10 12:12", "12:12:00",
NA54, 536446, "12/1/10 12:15", "12:15:00",
NA55, 536460, "12/1/10 12:22", "12:22:00",
NA56, 536463, "12/1/10 12:22", "12:22:00",
NA57, 536464, "12/1/10 12:23", "12:23:00",
NA58, 536466, "12/1/10 12:23", "12:23:00",
NA59, 536477, "12/1/10 12:27", "12:27:00",
NA60, 536488, "12/1/10 12:31", "12:31:00",
NA61, 536500, "12/1/10 12:35", "12:35:00",
NA62, 536502, "12/1/10 12:36", "12:36:00",
NA64, 536508, "12/1/10 12:38", "12:38:00",
NA65, 536514, "12/1/10 12:40", "12:40:00",
NA66, 536520, "12/1/10 12:43", "12:43:00",
NA67, 536521, "12/1/10 12:48", "12:48:00",
NA68, 536522, "12/1/10 12:49", "12:49:00",
NA69, 536523, "12/1/10 12:50", "12:50:00",
NA70, 536524, "12/1/10 12:51", "12:51:00",
NA71, 536525, "12/1/10 12:54", "12:54:00",
NA72, 536526, "12/1/10 12:58", "12:58:00",
NA73, 536527, "12/1/10 13:04", "13:04:00",
NA74, 536528, "12/1/10 13:17", "13:17:00",
NA75, 536529, "12/1/10 13:20", "13:20:00",
NA76, 536530, "12/1/10 13:21", "13:21:00",
NA77, 536531, "12/1/10 13:23", "13:23:00",
NA78, 536532, "12/1/10 13:24", "13:24:00",
NA79, 536533, "12/1/10 13:31", "13:31:00",
NA80, 536534, "12/1/10 13:33", "13:33:00",
NA81, 536535, "12/1/10 13:38", "13:38:00",
NA82, 536536, "12/1/10 13:45", "13:45:00",
NA83, 536537, "12/1/10 13:51", "13:51:00",
NA84, 536538, "12/1/10 13:54", "13:54:00",
NA85, 536539, "12/1/10 14:03", "14:03:00",
NA86, 536540, "12/1/10 14:05", "14:05:00",
NA87, 536541, "12/1/10 14:05", "14:05:00",
NA88, 536542, "12/1/10 14:11", "14:11:00",
NA90, 536544, "12/1/10 14:32", "14:32:00",
NA91, 536545, "12/1/10 14:32", "14:32:00",
NA93, 536546, "12/1/10 14:33", "14:33:00",
NA94, 536547, "12/1/10 14:33", "14:33:00",
NA95, 536549, "12/1/10 14:34", "14:34:00",
NA96, 536550, "12/1/10 14:34", "14:34:00",
NA97, 536551, "12/1/10 14:34", "14:34:00",
NA98, 536552, "12/1/10 14:34", "14:34:00",
NA99, 536553, "12/1/10 14:35", "14:35:00",
NA100, 536554, "12/1/10 14:35", "14:35:00",
NA101, 536555, "12/1/10 14:37", "14:37:00",
NA102, 536556, "12/1/10 14:38", "14:38:00",
NA103, 536557, "12/1/10 14:41", "14:41:00",
NA104, 536558, "12/1/10 14:48", "14:48:00",
NA105, 536559, "12/1/10 14:54", "14:54:00",
NA106, 536560, "12/1/10 15:00", "15:00:00",
NA107, 536561, "12/1/10 15:06", "15:06:00",
NA108, 536562, "12/1/10 15:08", "15:08:00",
NA109, 536563, "12/1/10 15:08", "15:08:00",
NA110, 536564, "12/1/10 15:08", "15:08:00",
NA111, 536565, "12/1/10 15:15", "15:15:00",
NA112, 536566, "12/1/10 15:21", "15:21:00",
NA113, 536567, "12/1/10 15:27", "15:27:00",
NA114, 536568, "12/1/10 15:28", "15:28:00",
NA115, 536569, "12/1/10 15:35", "15:35:00",
NA116, 536570, "12/1/10 15:35", "15:35:00",
NA117, 536571, "12/1/10 15:37", "15:37:00",
NA118, 536572, "12/1/10 15:40", "15:40:00",
NA119, 536573, "12/1/10 15:45", "15:45:00",
NA120, 536574, "12/1/10 15:46", "15:46:00",
NA121, 536575, "12/1/10 16:01", "16:01:00",
NA122, 536576, "12/1/10 16:11", "16:11:00",
NA123, 536577, "12/1/10 16:13", "16:13:00",
NA124, 536578, "12/1/10 16:15", "16:15:00",
NA125, 536579, "12/1/10 16:16", "16:16:00",
NA126, 536580, "12/1/10 16:17", "16:17:00",
NA127, 536581, "12/1/10 16:19", "16:19:00",
NA128, 536582, "12/1/10 16:21", "16:21:00",
NA129, 536583, "12/1/10 16:21", "16:21:00",
NA130, 536584, "12/1/10 16:22", "16:22:00",
NA131, 536585, "12/1/10 16:24", "16:24:00",
NA132, 536586, "12/1/10 16:25", "16:25:00",
NA133, 536587, "12/1/10 16:33", "16:33:00",
NA134, 536588, "12/1/10 16:49", "16:49:00",
NA135, 536589, "12/1/10 16:50", "16:50:00",
NA136, 536590, "12/1/10 16:52", "16:52:00",
NA137, 536591, "12/1/10 16:57", "16:57:00",
NA138, 536591, "12/1/10 16:58", "16:58:00",
NA139, 536592, "12/1/10 17:06", "17:06:00",
NA140, 536593, "12/1/10 17:15", "17:15:00",
NA141, 536594, "12/1/10 17:22", "17:22:00",
NA142, 536595, "12/1/10 17:24", "17:24:00",
NA143, 536596, "12/1/10 17:29", "17:29:00",
NA144, 536597, "12/1/10 17:35", "17:35:00",
NA145, 536598, "12/2/10 7:48", "7:48:00",
NA146, 536599, "12/2/10 7:49", "7:49:00",
NA147, 536600, "12/2/10 8:32", "8:32:00",
NA148, 536601, "12/2/10 8:33", "8:33:00",
NA149, 536602, "12/2/10 8:34", "8:34:00",
NA150, 536603, "12/2/10 8:40", "8:40:00",
NA151, 536604, "12/2/10 8:43", "8:43:00",
NA152, 536605, "12/2/10 8:52", "8:52:00",
NA154, 536607, "12/2/10 9:29", "9:29:00",
NA155, 536608, "12/2/10 9:37", "9:37:00",
NA156, 536609, "12/2/10 9:41", "9:41:00",
NA157, 536610, "12/2/10 9:42", "9:42:00",
NA158, 536611, "12/2/10 9:43", "9:43:00",
NA159, 536612, "12/2/10 9:44", "9:44:00",
NA160, 536613, "12/2/10 9:44", "9:44:00"
)
The data is from transactions in a retail store. I am calculating transaction wait time for three scenarios, "month", "weekday" and "hour", which is the difference in time given by "transTime" between two consecutive rows with the following code:
# Plot customer wait times
output$customerWait <- renderPlot({
req(credentials()$user_auth)
withProgress(message = 'Calculation in progress',
detail = 'This may take a while...', value = 0, {
for (i in 1:15) {
incProgress(1/15)
Sys.sleep(30)
}
})
# Graph customer waiting time by month
customerWait <- customerWait() %>%
mutate(tDate=as.Date(Date)) %>%
filter(tDate >= as.Date(input$dRange[1]) & tDate <= as.Date(input$dRange[2])) %>%
mutate(Month=month(Date, label = TRUE)) %>%
# mutate(Month=as.factor(month(Date))) %>%
mutate(transTime = as.hms(transTime)) %>%
mutate(elapsed = transTime - lag(transTime, default = first(transTime))) %>%
filter(elapsed > 0) %>%
group_by(Month) %>%
summarise(Mean_Wait_Time = mean(elapsed, na.rm = TRUE)) %>%
mutate(Mean_Wait_Time = round(Mean_Wait_Time/60, digits = 2))
g1 <- ggplot(customerWait, aes(x=Month, y = Mean_Wait_Time, fill = Month)) +
geom_bar(stat="identity") +
# geom_label(aes(label=format(Mean_Wait_Time,big.mark = ",")))+
theme(legend.position="none")+
theme(panel.background = element_blank())+
labs(x = "Month", y = "Wait Time (minutes)", title = "Customer mean wait time by Month") +
labs(title="Monthly mean customer wait time")
# Graph customer waiting time by day
customerWait <- customerWait() %>%
mutate(tDate=as.Date(Date)) %>%
filter(tDate >= as.Date(input$dRange[1]) & tDate <= as.Date(input$dRange[2])) %>%
mutate(Day = as.factor(weekdays(as.Date(tDate)))) %>%
mutate(transTime = as.hms(transTime)) %>%
mutate(elapsed = transTime - lag(transTime, default = first(transTime))) %>%
filter(elapsed > 0) %>%
group_by(Day) %>%
summarise(Mean_Wait_Time = mean(elapsed, na.rm = TRUE)) %>%
mutate(Mean_Wait_Time = round(Mean_Wait_Time/60, digits = 2))
g2 <- ggplot(customerWait, aes(x=Day, y = Mean_Wait_Time, fill = Day)) +
geom_bar(stat="identity") +
theme_classic() +
# geom_label(aes(label=format(Mean_Wait_Time, big.mark = ",")))+
labs(
x="Day",
y="Mean Wait Time (minutes)",
title = paste(
"Mean Customer Wait Time per weekday"
)
)+
theme(legend.position="none") +
scale_x_discrete(limits=c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
# Graph customer waiting time by hour
customerWait <- customerWait() %>%
mutate(tDate=as.Date(Date)) %>%
filter(tDate >= as.Date(input$dRange[1]) & tDate <= as.Date(input$dRange[2])) %>%
mutate(Hour = as.factor(hour(Date))) %>%
mutate(transTime = as.hms(transTime)) %>%
mutate(elapsed = transTime - lag(transTime, default = first(transTime))) %>%
filter(elapsed > 0) %>%
group_by(Hour) %>%
summarise(Mean_Wait_Time = mean(elapsed, na.rm = TRUE)) %>%
mutate(Mean_Wait_Time = round(Mean_Wait_Time/60, digits = 2))
g3 <- ggplot(customerWait, aes(x=Hour, y = Mean_Wait_Time, fill = Hour)) +
geom_bar(stat="identity") +
# geom_label(aes(label=format(Mean_Wait_Time,big.mark = ",")))+
theme(legend.position="none")+
theme(panel.background = element_blank())+
labs(x = "Hour", y = "Wait Time (minutes)", title = "Mean Customer wait time by Hour") +
labs(title="Hourly mean customer wait time")
grid.arrange(g1, g2, g3)
}, height = 600, width = 800)
However, this code includes time at the end of the day to the next day when the retail outlet is closed thus giving wrong transaction times. How can i exclude the times when the retail outlet is closed in my code above?
The circled transaction below illustrates the end of day change to the next morning which should not be included in the calculations.
The expected output is as follows:
Regards,
Chris