Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide two difftime objects

Tags:

datetime

r

I have three time (POSIXct) objects t1, t2, t3 which specify the time duration to complete a task.

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")

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

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

like image 955
Katherine Avatar asked Oct 24 '16 21:10

Katherine


People also ask

How to use double difftime () function in C++?

difftime() function in C++. The difftime() function is defined in ctime header file. The difftime() function is used to calculate the difference between two times in second. Syntax: double difftime(time_t end, time_t start); Parameters: This method accepts two parameters: start: time_t object for start time. end: time_t object for end time.

How to calculate the difference between two times in second in ctime?

The difftime () function is defined in ctime header file. The difftime () function is used to calculate the difference between two times in second. Parameters: This method accepts two parameters: start: time_t object for start time. end: time_t object for end time. Returns: This function returns the difference between two times in seconds.

What is the function difftime?

Function difftime calculates a difference of two date/time objects and returns an object of class "difftime" with an attribute indicating the units.

How do you use difftime in R?

Basic R Syntax: difftime (time_1, time_2) The difftime R function calculates the time difference of two date or time objects. The basic syntax for difftime in R is shown above.


3 Answers

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')

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.

like image 151
Matthew Lundberg Avatar answered Oct 16 '22 14:10

Matthew Lundberg


As of today's date (9/2018), you can use as.numeric() to turn your difftime values into numeric values. ie, if you were to take

as.numeric(t3)

R will return 10 as desired.

like image 44
obewanjacobi Avatar answered Oct 16 '22 14:10

obewanjacobi


@MatthewLundberg's answer is more correct, but I'll suggest an alternative just to help illustrate the underlying structures of time-based objects in R are always just numbers:

unclass(t1)/unclass(t3)
# [1] 1.8
# attr(,"units")
# [1] "mins"

Note that, in terms of units, the approach of t1/as.double(t3, units = 'secs') doesn't make much sense, since the units of the output are min/sec, whereas this answer is unit-free.

Note further that this approach is a bit dangerous, since by default, -.POSIXt (which is ultimately called when you define t1, t2, and t3) automatically chooses the units of the output (at core, -.POSIXt here will call difftime with the default units = "auto"). In this case, we are (maybe) lucky that all 3 are given units, but consider t4:

t4 = as.POSIXct('2017-10-21 12:00:35') - as.POSIXct('2017-10-21 12:00:00')
t4
# Time difference of 35 secs

Again, if we use t4 in a ratio, we'll probably get the wrong units.

We can avoid this by explicitly calling difftime and declaring the units upfront:

t1 = difftime("2016-10-30 13:53:34", "2016-10-30 13:35:34", units = 'mins')
t2 = difftime("2016-10-30 14:53:34", "2016-10-30 14:35:34", units = 'mins')
t3 = difftime("2016-10-30 15:50:34", "2016-10-30 15:40:34", units = 'mins')
t4 = difftime('2017-10-21 12:00:35', '2017-10-21 12:00:00', units = 'mins')
t4
# Time difference of 0.5833333 mins
like image 3
MichaelChirico Avatar answered Oct 16 '22 15:10

MichaelChirico