I have a lot of data where every row has a time stamp. I want to make a vector with the time difference between rows. Here is a dummy time sequence:
start.day <- as.POSIXct("2010-11-25 04:13:00 WET")
end.day <- as.POSIXct("2016-11-25 16:25:35 WET")
days.seq <- seq.POSIXt(start.day,end.day,'mins')
days.seq <- sample(days.seq,100000)
days.seq <- days.seq[order(days.seq)]
I started with a for
loop but it takes ages for the loop to do the calculations:
start.time <- Sys.time()
time.diff <- NULL
for (i in 2:length(days.seq)) {
time.diff[i]<- difftime(days.seq[i],days.seq[i-1],units = 'mins')
}
time.diff[1] <- 0
time.diff
end.time <- Sys.time()
difftime(end.time,start.time,'secs')
How can I make R do this faster?