[R-SIG-Finance] xts and Sys.time() - very stange behaviour

Joshua Ulrich josh.m.ulrich at gmail.com
Wed Aug 1 20:39:31 CEST 2012


This is an issue with precision and formatting when you convert to
character.  Here's a truly reproducible example:

require(xts)
options(digits.secs=6)
x <- xts(1, .POSIXct(1343845073.618 + 1e-3))
# Note that the index is correct, given the construction above
sprintf("%15.5f",.index(x))
# But also note that it doesn't print "correctly"
# (due to rounding/precision issues)
print(x)
# You can see how it's actually stored by using format
key <- as.character(index(x), format="%Y-%m-%d %H:%M:%OS6")
# There's the problem
print(key)

I don't know of a solution, other than "don't do that".

Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com


On Wed, Aug 1, 2012 at 1:08 PM, soren wilkening <me at censix.com> wrote:
> require(xts)
> options(digits.secs=3)
>
> ob <- xts(as.matrix(t(c("aa", "bb"))), order.by = as.POSIXct("2000-01-31
> 13:45:02.437") )
> #get t0 as an arbitrary timestamp, with subsecond accuracy, truncated to
> microseconds
> t0 <- as.POSIXct( strftime(Sys.time(), "%Y-%m-%d %H:%M:%OS3") )
> #add a microsecond
> ordertime1 <- t0 + 1e-3
> neworder <- xts(as.matrix(t(c('aaaa', 'bbbb'))), order.by = (ordertime1))
> ob <- rbind(ob,neworder)
> key <- as.character(index(neworder))
>
> #returns 1 row
> ob[ "2000-01-31 13:45:02.437" ]
>
> # sometimes returns nothing ???
> ob[ key ]
>
>
>
>
>
> The last line should normally return the last row of 'ob'. If I run the
> entire code snippet about 5 times
> I get at least one run where it returns an empty row. But that should never
> happen.?! Here is my sessionInfo.
>
> 1> sessionInfo()
> R version 2.12.0 (2010-10-15)
> Platform: i686-pc-linux-gnu (32-bit)
>
> locale:
>  [1] LC_CTYPE=en_US.utf8       LC_NUMERIC=C
>  [3] LC_TIME=en_US.utf8        LC_COLLATE=en_US.utf8
>  [5] LC_MONETARY=C             LC_MESSAGES=en_US.utf8
>  [7] LC_PAPER=en_US.utf8       LC_NAME=C
>  [9] LC_ADDRESS=C              LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] xts_0.7-6.9 zoo_1.7-0
>
> loaded via a namespace (and not attached):
> [1] grid_2.12.0     lattice_0.19-13
>
>
> I have just noticed that when I change the line:
> ordertime1 <- t0 + 1e-3
> to
> ordertime1 <- t0
> Things seem to work fine. That however is not a solution since the addition
> is an essential component here.
>
>
>
> -----
> http://censix.com
> --
> View this message in context: http://r.789695.n4.nabble.com/xts-and-Sys-time-very-stange-behaviour-tp4638714p4638728.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



More information about the R-SIG-Finance mailing list