Search code examples
rnalagdifftime

In R, is there a way to get the time difference between two times in two different columns using lag, but ignoring all null values?


I have a dataframe that looks like this:

a<-data.frame(Time = c("Time1",
                   "Time2",
                   "Time3",
                   "Time4",
                   "Time5",
                   "Time6"),
          Start = c("2018-03-17 08:38:00",
                    NA,
                    NA,
                    NA,
                    NA,
                    NA),
          End = c(NA,
                  NA,
                  NA,
                  NA,
                  NA,
                  "2018-03-17 14:31:00"))

I am trying to add another column with the time difference between the "End" time and the "Start" time.

I've tried to use difftime(End, lag(Start), units = "mins"). But I can't seem to figure out how to ignore the null values to use the last non-null value in "Start".

This is how I expect the result to look:

b<-data.frame(Time = c("Time1",
                   "Time2",
                   "Time3",
                   "Time4",
                   "Time5",
                   "Time6"),
          Start = c("2018-03-17 08:38:00",
                    NA,
                    NA,
                    NA,
                    NA,
                    NA),
          End = c(NA,
                  NA,
                  NA,
                  NA,
                  NA,
                  "2018-03-17 14:31:00"),
          Time_Diff_min = c(rep(353, times = 6)))

Thank you!


Solution

  • Apply na.locf0 to both columns and subtract.

    library(zoo)
    transform(a, diff = as.numeric(difftime(na.locf0(as.POSIXct(End), fromLast = TRUE), 
                                            na.locf0(as.POSIXct(Start)),
                                            unit = "min")))
    

    giving:

       Time               Start                 End diff
    1 Time1 2018-03-17 08:38:00                <NA>  353
    2 Time2                <NA>                <NA>  353
    3 Time3                <NA>                <NA>  353
    4 Time4                <NA>                <NA>  353
    5 Time5                <NA>                <NA>  353
    6 Time6                <NA> 2018-03-17 14:31:00  353