【问题标题】:Subtracting three time columns in R减去 R 中的三个时间列
【发布时间】:2013-10-28 18:04:43
【问题描述】:

我有一个关于从 R 中减去时间列的问题。我需要将三列相互减去以获得总观察时间。我需要去做 (TIMEEND-TIMESTART)-TIMEOUT.

当我使用OBSTIME <- difftime(pipers$TIMEEND, pipers$TIMESTART, units = "hours") 时,我得到了第一部分,但是如何从中减去TIMEOUT。或者有没有办法同时减去所有三列?这甚至是正确的格式吗?

提前感谢您的帮助!

米歇尔

$ TIMESTART : POSIXct, format: "2013-10-28 10:30:00" "2013-10-28 14:50:00" "2013-10-28 14:50:00" ...
 $ TIMEEND   : POSIXct, format: "2013-10-28 12:30:00" "2013-10-28 16:50:00" "2013-10-28 16:50:00" ...
 $ TIMEOUT   : POSIXct, format: "2013-10-28 00:04:00" "2013-10-28 00:10:00" "2013-10-28 00:10:00" ..

【问题讨论】:

  • TIMEOUT 真的是持续时间吗?
  • 从 difftime 中减去日期时间值没有意义。

标签: r datetime time


【解决方案1】:

假设 TIMEOUT 最初是指一个期间,即观察回合中的“中断”,在某种程度上已转换为POSIXct。所以现在它显示为一个日期。好吧,鉴于我的猜测与现实有关,您可以尝试这样的事情。在这里,我使用包 lubridate 中的函数 hourminute 来提取“out”对象的相关(我的猜测......)部分。

library(lubridate)

# using your first set of times/periods as example
end <- as.POSIXct("2013-10-28 12:30:00")
start <- as.POSIXct("2013-10-28 10:30:00")
out <- as.POSIXct("2013-10-28 00:04:00")

# calculate total duration as difference between 'end' and 'start', in units minutes
tot <- as.numeric(difftime(end, start, units = "mins")) 

# calculate break in minutes using hour and minute components extracted from 'out'.
out_min <- (hour(out) * 60 + minute(out))

# calculate net duration of observation
tot - out_min
# [1] 116

当然,如果 'TIMEOUT' 中有额外的时间单位,这需要调整,例如几秒钟或几天。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    相关资源
    最近更新 更多