在 R 中,有没有办法使用滞后来获取两个不同列中两次之间的时间差,但忽略所有空值?

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?

我有一个如下所示的数据框:

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"))

我正在尝试添加另一列,其中包含 "End" 时间和 "Start" 时间之间的时差。

我试过使用 difftime(End, lag(Start), units = "mins")。 但我似乎无法弄清楚如何忽略空值以使用 "Start".

中的最后一个非空值

这是我期望的结果:

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)))

谢谢!

na.locf0应用于两列并减去。

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

给予:

   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