R xts: .001 milliseconds in index

It seems that POSIXlt allows the millisecond precision specification, but I have a problem setting the index value .001 of the millisecond index in the xts object:

> options(digits.secs = 3) > data(sample_matrix) > sample.xts = xts(sample_matrix, rep(as.POSIXlt("2012-03-20 09:02:50.001"), 180)) > head(sample.xts, 10) Open High Low Close 2012-03-20 09:02:50.000 50.03978 50.11778 49.95041 50.11778 2012-03-20 09:02:50.000 50.23050 50.42188 50.23050 50.39767 2012-03-20 09:02:50.000 50.42096 50.42096 50.26414 50.33236 2012-03-20 09:02:50.000 50.37347 50.37347 50.22103 50.33459 2012-03-20 09:02:50.000 50.24433 50.24433 50.11121 50.18112 2012-03-20 09:02:50.000 50.13211 50.21561 49.99185 49.99185 2012-03-20 09:02:50.000 50.03555 50.10363 49.96971 49.98806 2012-03-20 09:02:50.000 49.99489 49.99489 49.80454 49.91333 2012-03-20 09:02:50.000 49.91228 50.13053 49.91228 49.97246 2012-03-20 09:02:50.000 49.88529 50.23910 49.88529 50.23910 > sample.xts = xts(sample_matrix, rep(as.POSIXlt("2012-03-20 09:02:50.002"), 180)) > head(sample.xts, 10) Open High Low Close 2012-03-20 09:02:50.002 50.03978 50.11778 49.95041 50.11778 2012-03-20 09:02:50.002 50.23050 50.42188 50.23050 50.39767 2012-03-20 09:02:50.002 50.42096 50.42096 50.26414 50.33236 2012-03-20 09:02:50.002 50.37347 50.37347 50.22103 50.33459 2012-03-20 09:02:50.002 50.24433 50.24433 50.11121 50.18112 2012-03-20 09:02:50.002 50.13211 50.21561 49.99185 49.99185 2012-03-20 09:02:50.002 50.03555 50.10363 49.96971 49.98806 2012-03-20 09:02:50.002 49.99489 49.99489 49.80454 49.91333 2012-03-20 09:02:50.002 49.91228 50.13053 49.91228 49.97246 2012-03-20 09:02:50.002 49.88529 50.23910 49.88529 50.23910 

Why is parameter 001 milliseconds not working?

+2
source share
2 answers

I suspect this is a rounding / floating point issue:

 Browse[2]> print(head(as.numeric(order.by)), digits = 20) [1] 1332234170.0009999275 1332234170.0009999275 1332234170.0009999275 [4] 1332234170.0009999275 1332234170.0009999275 1332234170.0009999275 

This was achieved by debugging xts() when called

 foo <- xts(1:180, rep(as.POSIXlt("2012-03-20 09:02:50.001"), 180), unqiue = FALSE) 

but you can clearly see the problem through

 > print(as.numeric(as.POSIXlt("2012-03-20 09:02:50.001"))) [1] 1332234170 > print(as.numeric(as.POSIXlt("2012-03-20 09:02:50.001")), digits = 20) [1] 1332234170.0009999275 

Indicating that your fractional number of seconds cannot be created and not saved exactly .001 milliseconds. If truncation like 3 dp will contain .002 as it is saved as:

 > print(as.numeric(as.POSIXlt("2012-03-20 09:02:50.002")), digits = 20) [1] 1332234170.0020000935 

Truncating or rounding to 3 dp will save the .002 part. One of the problems you have to deal with when working with computers.

Note that this seems to be a problem in the printed representation of indexes:

 > print(as.numeric(index(foo)[1]), digits = 20) [1] 1332234170.0009999275 

Accuracy (with floating point problems) is stored in the actual object storing index times - you simply cannot see this when printing time to the console.

+2
source

The POSIXct view is a very clever โ€œhackโ€ by Professor Ripley, who breaks the standard double-byte word into โ€œstill enoughโ€ for the usual โ€œnumber of days since the era,โ€ plus โ€œa suitable amount of accuracy for subsecond data.โ€ It works for about a microsecond:

 R> now <- Sys.time() R> for (x in seq(1,10)) print(difftime(now, now + 10^-x)) Time difference of -0.0999999 secs Time difference of -0.00999999 secs Time difference of -0.000999928 secs Time difference of -9.98974e-05 secs Time difference of -1.00136e-05 secs Time difference of -9.53674e-07 secs Time difference of 0 secs Time difference of 0 secs Time difference of 0 secs Time difference of 0 secs R> 
+4
source

Source: https://habr.com/ru/post/906175/


All Articles