[R-SIG-Finance] Milliseconds to proper date/time class

Dirk Eddelbuettel edd at debian.org
Fri Feb 8 15:45:04 CET 2008


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.



More information about the R-SIG-Finance mailing list