Convert time interval to number in R

Published on

Let’s say you have two time values in R, t1 and t2, and you want to compute the distance between them.

A natural thing to try would be

This should probably return a number in seconds, but it might be in some other units, so you test just to be safe:

“Ok,” you think, “seconds it is,” and proceed to use an expression like the above in your code.

You just fell victim to an incredibly nasty “feature” of the R date/time API. The as.numeric and other similar functions actually choose the units based on the length of the interval by default:

To get a consistent behavior, you need to specify the units, e.g.:

The acceptable units values are "auto", "secs", "mins", "hours", "days", and "weeks".