0

I want to create a regular sequence from a start date (e.g. "2021-11-19 09:28:57.95 UTC") to an end date (e.g. "2021-11-20 00:24:21.96 UTC") with time steps of 0.01 seconds.

x1 <- as.POSIXct(strptime("2021-11-19 09:28:57.95", "%Y-%m-%d %H:%M:%OS"), tz = "UTC")
x2 <- as.POSIXct(strptime("2021-11-20 00:24:21.96", "%Y-%m-%d %H:%M:%OS"), tz = "UTC")
testseq <- data.frame(y = seq(x1, x2, by = 0.01))

But the generated sequence often has irregularities. Instead of having a difference of exactly 0.01 seconds, the time difference between two points can be 0.00999999, 0.01000001 or 0.01000023 seconds. I can't find the source of these irregularities and I need to have a perfectly regular sequence with time steps of 0.01 seconds. Here is an illustration of the problem.

A part of the irregular sequence generated The difftime show the decimals problems

I just want the find a way to create a perfectly regular sequence with a level of precision of 0.01 second. I would be grateful if you could help me with this.

L3o
  • 1
  • It's not possible with `POSIXct` values. They are stored as floating point numbers. For an overview of why floating point numbers can be messy, see [why aren't these numbers equal](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal). If you need that level of precision, you'll likely want to store the parts of the date/time variable separately. Keep the time as the number of milliseconds since midnight or something similar -- use an integer values rather than floating point one to avoid loss of precision. – MrFlick Sep 09 '22 at 15:46

0 Answers0