Thorsten
Thorsten

Reputation: 144

R cumulative time difference

I would like to calculate the cumulative time difference from the start. I coded a rough solution, which I do not particularly like. Does somebody have a more elegant and reliable solution which can be used in dplyr pipe? The desires result shall be as in the diffCum column.

require(dplyr)

d = data.frame(n = 1:3, t = lubridate::ymd_hms("2020-03-30 08:15:39","2020-03-30 10:15:39","2020-03-30 14:15:39")) %>%
  mutate(diffMin = difftime(t, lag(t,1), unit = "mins")) %>%
  mutate(diffMin = ifelse(is.na(diffMin), 0, diffMin)) %>% # error prone as it would capture other NAs 
  mutate(diffCum = cumsum(diffMin)) # does not work with difftime class

Upvotes: 4

Views: 836

Answers (1)

Random
Random

Reputation: 131

I'm unsure of what you mean by "capturing other NAs" and I'm also unsure if this qualifies as elegant!

d <- 
  data.frame(n = 1:3, t = lubridate::ymd_hms("2020-03-30 08:15:39","2020-03-30 10:15:39","2020-03-30 14:15:39")) %>%
  mutate(
    diffMin = difftime(t, lag(t,1, default = t[1] ), unit = "mins") %>% 
      as.numeric() %>%
      cumsum()
    )

Upvotes: 3

Related Questions