简体   繁体   English

在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?

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"). 我尝试使用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! 谢谢!

Apply na.locf0 to both columns and subtract. na.locf0应用于两列并相减。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM