I have the following data frame which gives a transaction id (Transaction) and time of transaction (transTime) for a retail outlet:
I am calculating time elapsed from one transaction to the next based on the following code:
mutate(elapsed = transTime - lag(transTime, default = first(transTime)))
However, this calculation becomes erroneous at the end of the day when a retail outlet closes shop at the end of the day and resumes business the next day. For example, the circled consecutive transactions, 536597 & 536598 highlight the moving from the end of the day on the 1st of December 2010 to the start of another day on the 2nd of December 2010 should not the used in the calculation of time elapsed between these two transactions as it would be meaningless as the retail outlet would have been closed and such a calculation would be meaningless and gives an erroneous very high elapsed time. The calculation should restart the next morning the retail shop opens, i.e. on the next day.
Is there a way of changing my code to take care of this?