Dwell time / Time difference start recounting after a significant time gap

Viewed 49

I am trying to calculate dwell time per location. However, when ID #1 leaves a location at 23:30:36 the night before and comes back to the same location the day after, my code accumulates the time difference, though it should basically start recounting the next day. A cutoff overnight would not do the job because sometimes these gaps happen during daytime as well.

Is there a way the time difference starts recounting after a significant gap? This same problem in fact also happens between location B and C, though it is only a 4.6 minutes gap.

location    ID  time                 time difference (mins)
A            1  2013-03-27 23:29:12  0.516666667
A            1  2013-03-27 23:29:43  0.466666667
A            1  2013-03-27 23:30:11  0.25
B            1  2013-03-27 23:30:26  0.166666667
B            1  2013-03-27 23:30:36  *794.6*
B            1  2013-03-28 12:45:12  0.25
B            1  2013-03-28 12:45:27  4.633333333
C            1  2013-03-28 12:50:05  0.25
C            1  2013-03-28 12:50:20  1.383333333
C            1  2013-03-28 12:51:43  0.266666667

Current code:

df <- df %>%
  mutate(diff = c(difftime(tail(df$time, -1), head(df$time, -1), units = "mins"),0))
0 Answers
Related