如何查找数据框中两个日期/时间列之间的时间差

时间:2018-06-05 15:48:05

标签: r

我有一个如下数据框,

S.no          f_req_time         f_drop_time
1    2016-07-11 06:04:00   2016-07-11 06:44:00
2    2016-07-11 12:20:00   2016-07-11 13:10:00
3    2016-07-11 16:19:00   2016-07-11 17:25:00
4    2016-07-12 09:03:00   2016-07-12 09:58:00
5    2016-07-12 12:10:00   2016-07-12 12:49:00

我想添加一个名为“等待时间”的列,这将是列的第一个值与时间的差异f_drop_time'(2016-07-11 06:44:00)即第一个值S.no.1和第二个值,即来自S.no.2 of f_req_time'(2016-07-11 12:20:00)。我怎样才能列出所有差异time.I尝试了for循环。它返回所有null。

日期时间列为POSIXct格式

我的代码,

funtion<-for (i in 1:nrow(driver_27)) {
  driver_27$wait <- driver_27$f_drop_time[i+1]-driver_27$f_req_time[i]
}

2 个答案:

答案 0 :(得分:0)

我们可以使用第一列的lead并使用difftime进行减法(请注意,不清楚unit

driver_27$wait <- with(driver_27, as.numeric(difftime(c(f_req_time[-1], NA),
                     f_drop_time, unit = 'hour')))
driver_27$wait
#[1]  5.60000  3.15000 15.63333  2.20000       NA

数据

driver_27 <- structure(list(S.no = 1:5, f_req_time = structure(c(1468231440, 
1468254000, 1468268340, 1468328580, 1468339800), class = c("POSIXct", 
"POSIXt"), tzone = ""), f_drop_time = structure(c(1468233840, 
1468257000, 1468272300, 1468331880, 1468342140), class = c("POSIXct", 
"POSIXt"), tzone = "")), .Names = c("S.no", "f_req_time", "f_drop_time"
), row.names = c(NA, -5L), class = "data.frame")

答案 1 :(得分:0)

此解决方案抵消f_drop_time并存储在新列中以便于验证/检查。它还使用lubridate::interval()来计算以小时为单位的等待时间:

# data
data.frame(
  f_req_time = c("2016-07-11 06:04:00" , "2016-07-11 12:20:00", "2016-07-11 16:19:00", "2016-07-12 09:03:00", "2016-07-12 12:10:00"),
  f_drop_time = c("2016-07-11 06:44:00", "2016-07-11 13:10:00", "2016-07-11 17:25:00", "2016-07-12 09:58:00", "2016-07-12 12:49:00"),
  stringsAsFactors = FALSE
) -> x

# create a new column that has ofset f_drop_time by 1
x %>% mutate(temp = c(f_drop_time[-1], NA)) -> x

# convert to lubridate format
ymd_hms(x$temp) -> x$temp
ymd_hms(x$f_drop_time) -> x$f_drop_time

# calculates the interval in hours in 'wait_time' column
(x %>% mutate(wait_time = interval(f_drop_time, temp)/hours(1)) -> x)

# removes temp
x[, !names(x) %in% c("temp")] -> x

生成(带有临时数据):

           f_req_time         f_drop_time                temp wait_time
1 2016-07-11 06:04:00 2016-07-11 06:44:00 2016-07-11 13:10:00  6.433333
2 2016-07-11 12:20:00 2016-07-11 13:10:00 2016-07-11 17:25:00  4.250000
3 2016-07-11 16:19:00 2016-07-11 17:25:00 2016-07-12 09:58:00 16.550000
4 2016-07-12 09:03:00 2016-07-12 09:58:00 2016-07-12 12:49:00  2.850000
5 2016-07-12 12:10:00 2016-07-12 12:49:00                <NA>        NA