Here is my example. I am reading the following file: sample_data
library(dplyr)
txt <- c(\'\"\", \"MDN\", \"Cl_Date\"\',
\'\"
ts <- ts %>% group_by(MDN) %>% arrange(Cl_Date) %>%
mutate(time_diff_2 = as.numeric(Cl_Date-lag(Cl_Date), units = 'mins'))
Convert the time difference to a numeric value. You can use units
argument to make the return values consistent.
According to @hadley here, the solution is to use lubridate instead of relying on base R.
This would be something like:
ts %>%
group_by(MDN) %>%
arrange(Cl_Date) %>%
mutate(as.duration(Cl_Date %--% lag(Cl_Date)))