0

I have a question about subtracting columns of time from R. I have three columns that I need to subtract from one another to get a total amount of observation time. I need to do (TIMEEND-TIMESTART)-TIMEOUT.

When I use OBSTIME <- difftime(pipers$TIMEEND, pipers$TIMESTART, units = "hours"), I get the first part, but how do I subtract the TIMEOUT from this. Or is there a way of subtraction all three columns at the same time? Is this even the correct format to be doing this in?

Thank in advance for the help!

Michelle

$ 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" ..
4

1 回答 1

0

假设TIMEOUT 最初指的是一个period,一个观察回合中的“休息”,它在某种程度上已经转换为POSIXct. 所以现在它显示为一个日期。好吧,鉴于我的猜测与现实有关,您可以尝试这样的事情。在这里,我使用函数hourminutefrom packagelubridate来提取“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' 中有额外的时间单位,例如秒或天,则需要进行调整。

于 2013-10-28T18:57:29.737 回答