I have a dataframe of ids and timestamps. I\'d like to calculate the difference between each sequential timestamp for an individual id.
My dataframe looks like this:
using data.table
data.table
library(data.table) library(dplyr) setDT(dat)[, time.difference := time - lag(time, 1L), by = id]
Like this:
dat %>% group_by(id) %>% mutate(time.difference = time - lag(time))