kathy kathy - 2 months ago 36
R Question

Divide two time objects in r

I have three time objects t1, t2, t3 which specifies the time duration to complete a task.
I found t1, t2, t3 by doing the following :

t1 <- (2016-10-30 13:53:34) - (2016-10-30 13:35:34)
t2 <- (2016-10-30 14:53:34) - (2016-10-30 14:35:34)
t3 <- (2016-10-30 15:50:34) - (2016-10-30 15:40:34)

I want to find the factor by dividing t1/t3 and t2/t3.

I get the following error: second argument of / cannot be a "difftime" object.

I understood that two difftime objects cannot be divided. Is there any way that I could find the result of dividing two diffTime objects.

Any help or suggestion is appreciated.


To divide by a difftime you must convert it to numeric. The conversion requires that you specify the units. For example:

t1/as.double(t3, units='secs')

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.