Hi!
Having some trouble with cumulative sums, any guidance appreciated.
I have some customer counter data, and I need to understand due to COVID how many customers are in the shop at any given moment. After the day is complete, I need it to reset to 0.
Below is the data.
tibble::tribble(
~DateValue, ~TimeValue, ~CustomersIn, ~CustomersOut, ~DoorsOpenPlan, ~DoorsOpenActual,
"1/1/2020", "4:00", 57, 18, "NULL", 1,
"1/1/2020", "4:15", 107, 115, "NULL", 6,
"1/1/2020", "4:30", 99, 119, "NULL", 8,
"1/1/2020", "4:45", 93, 78, "NULL", 7,
"1/2/2020", "4:00", 82, 36, "2", 2,
"1/2/2020", "4:15", 165, 146, "5", 7,
"1/2/2020", "4:30", 165, 199, "7", 9,
"1/2/2020", "4:45", 151, 150, "8", 8,
"1/3/2020", "4:00", 66, 21, "4", 1,
"1/3/2020", "4:15", 102, 119, "6", 7,
"1/3/2020", "4:30", 178, 175, "6", 9,
"1/3/2020", "4:45", 186, 184, "7", 9,
"1/3/2020", "5:00", 224, 206, "8", 9,
"1/3/2020", "5:15", 202, 213, "9", 10,
"1/3/2020", "5:30", 256, 244, "9", 11,
"1/3/2020", "5:45", 225, 233, "9", 10,
"1/3/2020", "6:00", 213, 226, "11", 11,
"1/3/2020", "6:15", 260, 278, "11", 11,
"1/3/2020", "6:30", 320, 275, "11", 12
)
The first step, I need is a check. Are columns 3,4,5,6 = NULL and if they are it should return a "TRUE" else "FALSE".
Following this, if the check column is TRUE it should return NULL else do a cumulative sum of the difference between Customers IN and Customers OUT.
I would like it to reset to 0 when the the Time is less than the row above.
Desired Output:
tibble::tribble(
~DateValue, ~TimeValue, ~CustomersIn, ~CustomersOut, ~DoorsOpenPlan, ~DoorsOpenActual, ~Flag, ~CustomersinStore,
"01/01/2020", "04:00", 57, 18, "NULL", 1, TRUE, "NULL",
"01/01/2020", "04:15", 107, 115, "NULL", 6, TRUE, "NULL",
"01/01/2020", "04:30", 99, 119, "NULL", 8, TRUE, "NULL",
"01/01/2020", "04:45", 93, 78, "NULL", 7, TRUE, "NULL",
"02/01/2020", "04:00", 82, 36, "2", 2, FALSE, "0",
"02/01/2020", "04:15", 165, 146, "5", 7, FALSE, "46",
"02/01/2020", "04:30", 165, 199, "7", 9, FALSE, "65",
"02/01/2020", "04:45", 151, 150, "8", 8, FALSE, "31",
"03/01/2020", "04:00", 66, 21, "4", 1, FALSE, "0",
"03/01/2020", "04:15", 102, 119, "6", 7, FALSE, "45",
"03/01/2020", "04:30", 178, 175, "6", 9, FALSE, "28",
"03/01/2020", "04:45", 186, 184, "7", 9, FALSE, "31",
"03/01/2020", "05:00", 224, 206, "8", 9, FALSE, "33",
"03/01/2020", "05:15", 202, 213, "9", 10, FALSE, "51",
"03/01/2020", "05:30", 256, 244, "9", 11, FALSE, "40",
"03/01/2020", "05:45", 225, 233, "9", 10, FALSE, "52",
"03/01/2020", "06:00", 213, 226, "11", 11, FALSE, "44",
"03/01/2020", "06:15", 260, 278, "11", 11, FALSE, "31",
"03/01/2020", "06:30", 320, 275, "11", 12, FALSE, "13"
)
Thanks all!