I have a couple of columns in a data frame with times. I'm trying to calculate the difference in times in a new column, but I need to reset it to zero every time I encounter the start of a new pattern. Please see some sample data below.
Seq ATime RT
0 18:33:00 0
20 18:48:00 15
43 19:01:00 13
56 19:47:00 24
0 21:33:00 0
9 21:45:00 12
22 21:55:00 10
45 22:13:00 18
0 06:33:00 0
22 06:47:00 14
45 06:59:00 12
62 07:22:00 23
85 07:48:00 26
I'm using the following script to estimate the delta column. The Seq column is always increasing for each 'pattern'. In this sample each pattern's Seq starts from 0, but it may not be the case always.
dat_4$RT <- 0
for (i in 1:(NROW(dat_4$Seq)-1)) {
if (dat_4$Seq[i+1] > dat_4$Seq[i]) {
dat_4$RT[i+1] = (chron(times=dat_4$ATime[i+1]) - chron(times=dat_4$ATime[i]))*1440
} else {
dat_4$RT[i+1] = 0
}
}
Although it works, it's not at all efficient. Sometimes the 'dat_4' dataframe will have roughly 4 million records and it takes almost 2.5 minutes just to process this step.
user system elapsed
96.86 54.07 150.99
Any suggestion on how I can make it more efficient?