Skip to content

millisec timestamps for rows of xts/zoo object

10 messages · Ulrich Staudinger, Brian G. Peterson, Gabor Grothendieck +1 more

#
On Sun, 2011-06-05 at 18:41 +0200, Ulrich Staudinger wrote:
How about just using the POSIXct mode?

options(digits.secs=6)

should do it.

see 

?POSIXct
#
On 5 June 2011 at 19:41, Ulrich Staudinger wrote:
| Misunderstanding ...
| 
| rowse[1]> options(digits.secs=6)
| Browse[1]> head(tempBs)
|                         ..1   ..2 ..3 pnlSlice
| 2011-06-03 13:32:24.975 242 99125   1      -75
| 2011-06-03 13:32:55.036 242 99100  -1      -25
| 2011-06-03 13:36:10.490 242 98850  -1      -50
| 2011-06-03 13:36:59.064 242 98775   2       25
| 2011-06-03 13:38:21.795 242 98550  -2     -200
| 2011-06-03 13:40:01.725 242 98825   1     -450
| Browse[1]>
| 
| 
| that's not what I need. A bit of digging revealed:
| 
| Browse[1]> as.double(as.POSIXct("2011-06-03 13:32:24"))
| [1] 1307100744
| Browse[1]>
| 
| I would like to get the timestamp instead of a date string ...
| some matrix where the first column contains the timestamp in milliseconds
| since epoch start ...

Just use as.numeric() on your POSIXct object:

R> print( as.numeric( Sys.time() ), digits=20 )
[1] 1307296635.9376249313
R> 


Dirk
#
On Sun, Jun 5, 2011 at 2:14 PM, Ulrich Staudinger <ustaudinger at gmail.com> wrote:
Try this assuming x is an xts object with POSIXct times:

z <- as.zoo(x)
time(z) <- as.numeric(time(z) - start(z))
#
On 5 June 2011 at 20:14, Ulrich Staudinger wrote:
| Hi Dirk,
| 
| but I don't have a POSIXct object, I have an xts time series object and want

Are you sure?  What does 

    class( index ( tempBs ) )

return?  And are you sure that time index for xts cannot be casted into POSIXct?

| the timestamps as seconds/milliseconds with the goal to save it to a database
| ...

Yes. POSIXct is 'just' a double with millseconds resolution which can be
passed down to C/C++, stored in a DB, etc pp with ease just because it can
reduce to a double (if you are willing to forego attributes such as local
timezones etc).

Dirk
1 day later
#
On 5 June 2011 at 22:49, Ulrich Staudinger wrote:
| Last question here, as.numeric(time(t)) returns seconds since 1970, but not the
| milliseconds, although I enabled it through setting the mentioned options. Any
| hint on that?

Wrong. You once again fell for displayed precision != stored precision:

Here is the example I use days ago:

   R> print( as.numeric( Sys.time() ), digits=20 )
   [1] 1307296635.9376249313
   R> 

whereas when I do it now without the digits= override:

   R> print( as.numeric( Sys.time() ) )
   [1] 1307477616
   R> 

Also:

   R> typeof( as.numeric( Sys.time() ) )
   [1] "double"
   R> 

Dirk