joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
219 stars 71 forks source link

Print trailing zeros for index with fractional seconds #404

Closed joshuaulrich closed 1 year ago

joshuaulrich commented 1 year ago

An object with an index that has sub-millisecond resolution may not print with the same number of decimal places for every index value. This is due to using as.character() instead of format().

library(xts)
set.seed(21)
n <- 1000
x <- .xts(rnorm(n), seq(1, n^2, n) + rnorm(n), dimnames = list(NULL, "x"))
print(x)
##                                      x
## 1969-12-31 18:00:01.793013  0.09516123
## 1969-12-31 18:16:41.522251 -0.03745645
## 1969-12-31 18:33:22.746222  0.92128291
## 1969-12-31 18:49:59.728664  1.66030628
##  1969-12-31 19:06:43.19739  0.79644836
## 1969-12-31 19:23:21.433131 -0.30404359
##   1969-12-31 19:39:59.4298 -1.43072982
## 1969-12-31 19:56:40.065094 -1.49423665
## 1969-12-31 20:13:21.063493  1.28826724
## 1969-12-31 20:30:00.997607 -0.48998283
##                        ...            
## 1970-01-12 05:00:00.809472  0.73573729
## 1970-01-12 05:16:39.574975 -2.49222511
## 1970-01-12 05:33:20.727859 -0.15759198
## 1970-01-12 05:50:01.061682  0.08606529
## 1970-01-12 06:06:41.125089  0.05555260
## 1970-01-12 06:23:20.961295 -1.27153534
## 1970-01-12 06:40:00.341953 -0.29615803
## 1970-01-12 06:56:40.378155 -0.37328966
##  1970-01-12 07:13:22.48765 -0.64532629
## 1970-01-12 07:30:01.368432 -0.37238591

# a more extreme example
y <- .xts(1:10, .POSIXct(c(1.0001, 2.002, 3.03, 4:9, 10.000001)),
          dimnames = list(NULL, "x"))
print(y, show.rows = 3, max.rows = 5)
##                             x
##   1969-12-31 18:00:01.0001  1
##    1969-12-31 18:00:02.002  2
##     1969-12-31 18:00:03.03  3
##                        ...   
##        1969-12-31 18:00:08  8
##        1969-12-31 18:00:09  9
## 1969-12-31 18:00:10.000001 10