-1

I'm looking to find faster and more accurate methods to find the difference between two times, preferably using the xts or zoo packages. At the moment I would like to find the number of milliseconds difference between two times, and to do this I have been using the difftimes function (with units set to seconds and I then multiply this number by 1000 to get milliseconds - for example: as.numeric(difftime(time2, time1, units = "secs")) * 1000).

This seems to work reasonably well, but I'm hoping for something faster and more accurate so I wish to use xts or zoo instead. However, I'm not sure how to go about finding the difference between two times using these packages.

Are they any particular functions in xts or zoo which can find the difference between two times? In addition, are they any functions which can allow me to compute the millisecond difference between two times?

EDIT: There's something important I forgot to include in my original topic: the reason why I want more accurate time methods is due to a rounding issue in my times. I am having a similar issue as given in this topic here: How R formats POSIXct with fractional seconds where my times are off by a fraction of a second (e.g. I get the time 11:12:53.332 rather than the time 11:12:53.333). Up until now I have 'fixed' this issue by including adding a smaller fraction of time to the number i.e. I can change 11:12:53.332 to 11:12:53.333 by writing 11:12:53.332 + 1e-6. However, I now need to go down to microseconds, so rather than 'fix' this issue by adding another fraction I would much rather use a package or function or something to achieve this task.

  • https://stackoverflow.com/questions/39131578/r-time-difference-between-2-columns-in-milliseconds – M-- Jul 14 '17 at 13:33

1 Answers1

1

I don think you need any of these packages. Run the following code:

a = Sys.time()
Sys.sleep(0.0001)
b = Sys.time()
difftime(b,a)

Now run the code again, but first run:

options(digits=20)

The object is stored with high accuracy, but just printed with a limited number of digits. Hope this helps!

Florian
  • 24,425
  • 4
  • 49
  • 80
  • Thanks for the response! Will including more significant figures bypass the 'rounding' issue that R has? I have a similar problem as given in this topic here: https://stackoverflow.com/questions/7726034/how-r-formats-posixct-with-fractional-seconds where I currently have 3 significant figures and I find that some times are slightly different (for instance, I have the time 11:12:53.332 rather than 11:12:53:333) – reallybadstatdude Jul 14 '17 at 22:09