Milliseconds to proper date/time class
A quick comment:
I think options('digits.secs'=6) might need to be set as well to be
able to see the fractional part of the second.
Jeff
On Feb 8, 2008 8:45 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
On 8 February 2008 at 10:49, guiseppe.milicia at hsbcib.com wrote: | Guys, | | I'm importing high frequency time series data into R. I'm using the zoo | timeseries class which works quite well. | | At the moment the times of my series are specified as milliseconds since | epoch. The issue is that the X axis in plots is not all that useful, the | same goes for things like window, etc. | | I was wondering what would be the reccomended way to convert from that | representation to a proper date/time representation that works with zoo, | e.g. Posixct? Not sure about "recommended" but here is one. Note that 'basic C type' is seconds since epoch, so you need to scale up from millisecs. Then given a vector of type "numeric" with timestamps in seconds since epoch (incl fractional seconds), you can obtain POSIXt object by adding the epoch itself -- and this is key -- as a proper POSIXt type, eg use this
options('width'=70)
xvec <- Sys.time() + sort(runif(4)*100)
xvec
[1] "2008-02-08 08:39:02.412544 CST" "2008-02-08 08:39:33.635201 CST" [3] "2008-02-08 08:39:44.867410 CST" "2008-02-08 08:39:52.813029 CST"
dvec <- as.numeric(xvec) dvec
[1] 1202481542 1202481574 1202481585 1202481593
diff(dvec) ## ie it really has fractional secs
[1] 31.22266 11.23221 7.94562
newXvec <- dvec + ISOdatetime(1970,1,1,0,0,0) - 6*60*60 ## Chicago is 6hrs off newXvec
[1] "2008-02-08 08:39:02.412544 CST" "2008-02-08 08:39:33.635201 CST" [3] "2008-02-08 08:39:44.867410 CST" "2008-02-08 08:39:52.813029 CST"
The GMT offset is a little tricky, but there are ways to work that out too. | SAVE PAPER - THINK BEFORE YOU PRINT! We should use that for r-help. SAVE ELECTRONS - THINK BEFORE YOU PRESS SEND! Dirk -- Three out of two people have difficulties with fractions.
_______________________________________________ R-SIG-Finance at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. -- If you want to post, subscribe first.
There's a way to do it better - find it. Thomas A. Edison