问题描述
我有三个时间( POSIXct )对象 t1 , t2 , t3 ,它们指定完成任务的持续时间。
I have three time (POSIXct) objects t1, t2, t3 which specify the time duration to complete a task.
我发现 t1 , t2 , t3 ,执行以下操作:
I found t1, t2, t3 by doing the following:
t1 <- as.POSIXct("2016-10-30 13:53:34") - as.POSIXct("2016-10-30 13:35:34") t2 <- as.POSIXct("2016-10-30 14:53:34") - as.POSIXct("2016-10-30 14:35:34") t3 <- as.POSIXct("2016-10-30 15:50:34") - as.POSIXct("2016-10-30 15:40:34")
我想找到比率 t1 / t3 和 t2 / t3 。但是,出现以下错误:
I want to find the ratios t1/t3 and t2/t3. However, I get the following error:
t1/t3 # Error in `/.difftime`(t1, t3) : # second argument of / cannot be a "difftime" object
无法分割两个 difftime 对象。有什么办法可以找到将两个 difftime 对象相除的结果?
I understood that two difftime objects cannot be divided. Is there any way that I could find the result of dividing two difftime objects?
推荐答案
要除以 difftime ,必须将其转换为数字。如您在评论中所述,如果您希望答案以秒为单位,则可以指定秒 单位。例如:
To divide by a difftime you must convert it to numeric. If, as you stated in a comment, you would like the answer to be expressed in seconds, you can specify the 'secs' units. For example:
t1/as.double(t3, units='secs')
如@JonathanLisic所述, as.double 通常不会占用 units 参数,这不适用于通用时间类。 difftime 的 S3 方法使用参数。
As @JonathanLisic notes, as.double does not generally take a units parameter, and this won't work for generic time classes. It is the S3 method for difftime which takes the parameter.
这篇关于划分两个difftime对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!