Transaction wait time - Time difference between rows in the same column - Shiny R

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

Here is a method for calculating the wait time but putting an NA for the lagged value at the beginning of each day. Wait time is in seconds in this version.

library(dplyr)
library(lubridate)

data <- tibble::tribble(
  ~Transaction,           ~Date, ~transTime,
  536365,  "12/1/10 8:26",  "8:26:00",
  536366,  "12/1/10 8:28",  "8:28:00",
  536367,  "12/1/10 8:34",  "8:34:00",
  536368,  "12/1/10 8:34",  "8:34:00",
  536369,  "12/1/10 8:35",  "8:35:00",
  536370,  "12/1/10 8:45",  "8:45:00",
  536371,  "12/1/10 9:00",  "9:00:00",
  536372,  "12/1/10 9:01",  "9:01:00",
  536373,  "12/1/10 9:02",  "9:02:00",
  536374,  "12/1/10 9:09",  "9:09:00",
  536375,  "12/1/10 9:32",  "9:32:00",
  536376,  "12/1/10 9:32",  "9:32:00",
  536377,  "12/1/10 9:34",  "9:34:00",
  536378,  "12/1/10 9:37",  "9:37:00",
  536380,  "12/1/10 9:41",  "9:41:00",
  536381,  "12/1/10 9:41",  "9:41:00",
  536382,  "12/1/10 9:45",  "9:45:00",
  536384,  "12/1/10 9:53",  "9:53:00",
  536385,  "12/1/10 9:56",  "9:56:00",
  536386,  "12/1/10 9:57",  "9:57:00",
  536387,  "12/1/10 9:58",  "9:58:00",
  536388,  "12/1/10 9:59",  "9:59:00",
  536389, "12/1/10 10:03", "10:03:00",
  536390, "12/1/10 10:19", "10:19:00",
  536392, "12/1/10 10:29", "10:29:00",
  536393, "12/1/10 10:37", "10:37:00",
  536394, "12/1/10 10:39", "10:39:00",
  536395, "12/1/10 10:47", "10:47:00",
  536396, "12/1/10 10:51", "10:51:00",
  536397, "12/1/10 10:51", "10:51:00",
  536398, "12/1/10 10:52", "10:52:00",
  536399, "12/1/10 10:52", "10:52:00",
  536400, "12/1/10 10:53", "10:53:00",
  536401, "12/1/10 11:21", "11:21:00",
  536402, "12/1/10 11:22", "11:22:00",
  536403, "12/1/10 11:27", "11:27:00",
  536404, "12/1/10 11:29", "11:29:00",
  536405, "12/1/10 11:32", "11:32:00",
  536406, "12/1/10 11:33", "11:33:00",
  536407, "12/1/10 11:34", "11:34:00",
  536408, "12/1/10 11:41", "11:41:00",
  536409, "12/1/10 11:45", "11:45:00",
  536412, "12/1/10 11:49", "11:49:00",
  536414, "12/1/10 11:52", "11:52:00",
  536415, "12/1/10 11:57", "11:57:00",
  536416, "12/1/10 11:58", "11:58:00",
  536420, "12/1/10 12:03", "12:03:00",
  536423, "12/1/10 12:08", "12:08:00",
  536425, "12/1/10 12:08", "12:08:00",
  536437, "12/1/10 12:12", "12:12:00",
  536446, "12/1/10 12:15", "12:15:00",
  536460, "12/1/10 12:22", "12:22:00",
  536463, "12/1/10 12:22", "12:22:00",
  536464, "12/1/10 12:23", "12:23:00",
  536466, "12/1/10 12:23", "12:23:00",
  536477, "12/1/10 12:27", "12:27:00",
  536488, "12/1/10 12:31", "12:31:00",
  536500, "12/1/10 12:35", "12:35:00",
  536502, "12/1/10 12:36", "12:36:00",
  536508, "12/1/10 12:38", "12:38:00",
  536514, "12/1/10 12:40", "12:40:00",
  536520, "12/1/10 12:43", "12:43:00",
  536521, "12/1/10 12:48", "12:48:00",
  536522, "12/1/10 12:49", "12:49:00",
  536523, "12/1/10 12:50", "12:50:00",
  536524, "12/1/10 12:51", "12:51:00",
  536525, "12/1/10 12:54", "12:54:00",
  536526, "12/1/10 12:58", "12:58:00",
  536527, "12/1/10 13:04", "13:04:00",
  536528, "12/1/10 13:17", "13:17:00",
  536529, "12/1/10 13:20", "13:20:00",
  536530, "12/1/10 13:21", "13:21:00",
  536531, "12/1/10 13:23", "13:23:00",
  536532, "12/1/10 13:24", "13:24:00",
  536533, "12/1/10 13:31", "13:31:00",
  536534, "12/1/10 13:33", "13:33:00",
  536535, "12/1/10 13:38", "13:38:00",
  536536, "12/1/10 13:45", "13:45:00",
  536537, "12/1/10 13:51", "13:51:00",
  536538, "12/1/10 13:54", "13:54:00",
  536539, "12/1/10 14:03", "14:03:00",
  536540, "12/1/10 14:05", "14:05:00",
  536541, "12/1/10 14:05", "14:05:00",
  536542, "12/1/10 14:11", "14:11:00",
  536544, "12/1/10 14:32", "14:32:00",
  536545, "12/1/10 14:32", "14:32:00",
  536546, "12/1/10 14:33", "14:33:00",
  536547, "12/1/10 14:33", "14:33:00",
  536549, "12/1/10 14:34", "14:34:00",
  536550, "12/1/10 14:34", "14:34:00",
  536551, "12/1/10 14:34", "14:34:00",
  536552, "12/1/10 14:34", "14:34:00",
  536553, "12/1/10 14:35", "14:35:00",
  536554, "12/1/10 14:35", "14:35:00",
  536555, "12/1/10 14:37", "14:37:00",
  536556, "12/1/10 14:38", "14:38:00",
  536557, "12/1/10 14:41", "14:41:00",
  536558, "12/1/10 14:48", "14:48:00",
  536559, "12/1/10 14:54", "14:54:00",
  536560, "12/1/10 15:00", "15:00:00",
  536561, "12/1/10 15:06", "15:06:00",
  536562, "12/1/10 15:08", "15:08:00",
  536563, "12/1/10 15:08", "15:08:00",
  536564, "12/1/10 15:08", "15:08:00",
  536565, "12/1/10 15:15", "15:15:00",
  536566, "12/1/10 15:21", "15:21:00",
  536567, "12/1/10 15:27", "15:27:00",
  536568, "12/1/10 15:28", "15:28:00",
  536569, "12/1/10 15:35", "15:35:00",
  536570, "12/1/10 15:35", "15:35:00",
  536571, "12/1/10 15:37", "15:37:00",
  536572, "12/1/10 15:40", "15:40:00",
  536573, "12/1/10 15:45", "15:45:00",
  536574, "12/1/10 15:46", "15:46:00",
  536575, "12/1/10 16:01", "16:01:00",
  536576, "12/1/10 16:11", "16:11:00",
  536577, "12/1/10 16:13", "16:13:00",
  536578, "12/1/10 16:15", "16:15:00",
  536579, "12/1/10 16:16", "16:16:00",
  536580, "12/1/10 16:17", "16:17:00",
  536581, "12/1/10 16:19", "16:19:00",
  536582, "12/1/10 16:21", "16:21:00",
  536583, "12/1/10 16:21", "16:21:00",
  536584, "12/1/10 16:22", "16:22:00",
  536585, "12/1/10 16:24", "16:24:00",
  536586, "12/1/10 16:25", "16:25:00",
  536587, "12/1/10 16:33", "16:33:00",
  536588, "12/1/10 16:49", "16:49:00",
  536589, "12/1/10 16:50", "16:50:00",
  536590, "12/1/10 16:52", "16:52:00",
  536591, "12/1/10 16:57", "16:57:00",
  536591, "12/1/10 16:58", "16:58:00",
  536592, "12/1/10 17:06", "17:06:00",
  536593, "12/1/10 17:15", "17:15:00",
  536594, "12/1/10 17:22", "17:22:00",
  536595, "12/1/10 17:24", "17:24:00",
  536596, "12/1/10 17:29", "17:29:00",
  536597, "12/1/10 17:35", "17:35:00",
  536598,  "12/2/10 7:48",  "7:48:00",
  536599,  "12/2/10 7:49",  "7:49:00",
  536600,  "12/2/10 8:32",  "8:32:00",
  536601,  "12/2/10 8:33",  "8:33:00",
  536602,  "12/2/10 8:34",  "8:34:00",
  536603,  "12/2/10 8:40",  "8:40:00",
  536604,  "12/2/10 8:43",  "8:43:00",
  536605,  "12/2/10 8:52",  "8:52:00",
  536607,  "12/2/10 9:29",  "9:29:00",
  536608,  "12/2/10 9:37",  "9:37:00",
  536609,  "12/2/10 9:41",  "9:41:00",
  536610,  "12/2/10 9:42",  "9:42:00",
  536611,  "12/2/10 9:43",  "9:43:00",
  536612,  "12/2/10 9:44",  "9:44:00",
  536613,  "12/2/10 9:44",  "9:44:00"
)
data <- mutate(data, Date = mdy_hm(Date))
data <- mutate(data, Day = as.Date(Date))
data <- group_by(data, Day) %>% mutate(DateShift = lag(Date))

data <- mutate(data, Wait = Date - DateShift)

Created on 2019-07-05 by the reprex package (v0.2.1)

1 Like

Many thanks for your input. I am however confused with the appearance of "data" in the above calculations. Can you kindly elaborate on this?

Regards

Chris

Are you confused by my use of data inside of the mutate() function? I placed data as the first argument of mutate rather than using the %>% operator to do that for me. The code could be written as the following.

data <- data %>% mutate(Date = mdy_hm(Date))
data <- data %>% mutate(Day = as.Date(Date))
data <- data %>% group_by(Day) %>% mutate(DateShift = lag(Date))

data <- data %>% mutate(Wait = Date - DateShift)

All of those calculations could have been done in a single chain but I broke out the steps so you can easily inspect what happens in each mutate()

Many thanks for your response, most appreciated. I have used the following code:

# Graph customer waiting time by month
        customerWait <- customerWait() %>% 
        mutate(Month=month(Date, label = TRUE)) %>%
        mutate(Date = mdy_h(Date)) %>%
        mutate(Day = as.Date(Date)) %>%
        group_by(Day) %>% mutate(DateShift = lag(Date)) %>%
        mutate(Wait = Date - DateShift) 
#> Error in customerWait() %>% mutate(Month = month(Date, label = TRUE)) %>% : could not find function "%>%"
      g1 <- ggplot(customerWait, aes(x=Month, y = Wait, fill = Month)) +
        geom_bar(stat="identity") +
        geom_label(aes(label=format(Wait,big.mark = ",")))+
        theme(legend.position="none")+
        theme(panel.background = element_blank())+
        labs(x = "Month", y = "Wait Time (seconds)", title = "Customer mean wait time by Month") +
        labs(title="Monthly customer wait time")
#> Error in ggplot(customerWait, aes(x = Month, y = Wait, fill = Month)): could not find function "ggplot"

Created on 2019-07-06 by the reprex package (v0.3.0)

I get a blank graph for the month plot. Where am i getting it wrong:

You get two errors that functions cannot be found. Have you loaded the libraries dplyr and ggplot2?

library(ggplot2)
library(dplyr)

Yes i have, i am using tidyverse for dplyr

# libraries ----
library(shiny)
library(arules)
#> Loading required package: Matrix
#> 
#> Attaching package: 'arules'
#> The following objects are masked from 'package:base':
#> 
#>     abbreviate, write
library(arulesViz)
#> Loading required package: grid
#> Registered S3 method overwritten by 'seriation':
#>   method         from 
#>   reorder.hclust gclus
library(tidyverse)
library(knitr)
library(gridExtra)
#> 
#> Attaching package: 'gridExtra'
#> The following object is masked from 'package:dplyr':
#> 
#>     combine
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
library(colorspace)
library(RColorBrewer)
library(DT)
#> 
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#> 
#>     dataTableOutput, renderDataTable
library(RMySQL)
#> Loading required package: DBI
library(DBI)
library(ggplot2)
library(plyr)
#> -------------------------------------------------------------------------
#> You have loaded plyr after dplyr - this is likely to cause problems.
#> If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
#> library(plyr); library(dplyr)
#> -------------------------------------------------------------------------
#> 
#> Attaching package: 'plyr'
#> The following object is masked from 'package:lubridate':
#> 
#>     here
#> The following objects are masked from 'package:dplyr':
#> 
#>     arrange, count, desc, failwith, id, mutate, rename, summarise,
#>     summarize
#> The following object is masked from 'package:purrr':
#> 
#>     compact
library(pool)
library(shinyjs)
#> 
#> Attaching package: 'shinyjs'
#> The following object is masked from 'package:pool':
#> 
#>     show
#> The following object is masked from 'package:RMySQL':
#> 
#>     show
#> The following object is masked from 'package:DBI':
#> 
#>     show
#> The following object is masked from 'package:colorspace':
#> 
#>     show
#> The following object is masked from 'package:lubridate':
#> 
#>     show
#> The following objects are masked from 'package:arules':
#> 
#>     info, show
#> The following object is masked from 'package:Matrix':
#> 
#>     show
#> The following object is masked from 'package:shiny':
#> 
#>     runExample
#> The following objects are masked from 'package:methods':
#> 
#>     removeClass, show
library(shinyauthr)
library(glue)
#> 
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse
library(cowplot)
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave
library(viridis)
#> Loading required package: viridisLite
library(ggridges)
#> 
#> Attaching package: 'ggridges'
#> The following object is masked from 'package:ggplot2':
#> 
#>     scale_discrete_manual
library(ggthemes)
#> 
#> Attaching package: 'ggthemes'
#> The following object is masked from 'package:cowplot':
#> 
#>     theme_map
library(tibble)
library(Rcpp)
library(shinycssloaders)
library(scales)
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:viridis':
#> 
#>     viridis_pal
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
library(visNetwork)
library(htmlwidgets)
library(plotly)
#> 
#> Attaching package: 'plotly'
#> The following objects are masked from 'package:plyr':
#> 
#>     arrange, mutate, rename, summarise
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(chron)
#> 
#> Attaching package: 'chron'
#> The following objects are masked from 'package:lubridate':
#> 
#>     days, hours, minutes, seconds, years
library(hms)
#> 
#> Attaching package: 'hms'
#> The following object is masked from 'package:lubridate':
#> 
#>     hms
library(anytime)

Created on 2019-07-07 by the reprex package (v0.3.0)

Running code inside reprex function is pointless unless you are providing a proper reproducible example that includes sample data, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one for a shiny app

1 Like