An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110605/4a1fc567/attachment.pl>
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:
I have an xts/zoo object called tempBs, which has nicely assigned the timestamps to each row (see below). How can I convert the row-label to seconds or milliseconds since the epoch beginning (or any other integer or double number)?
How about just using the POSIXct mode? options(digits.secs=6) should do it. see ?POSIXct
Brian G. Peterson http://braverock.com/brian/ Ph: 773-459-4973 IM: bgpbraverock
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110605/37fc1a3b/attachment.pl>
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
Gauss once played himself in a zero-sum game and won $50.
-- #11 at http://www.gaussfacts.com
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110605/e8ce1a69/attachment.pl>
On Sun, Jun 5, 2011 at 2:14 PM, Ulrich Staudinger <ustaudinger at gmail.com> wrote:
Hi Dirk, but I don't have a POSIXct object, I have an xts time series object and want the timestamps as seconds/milliseconds with the goal to save it to a database ...
Try this assuming x is an xts object with POSIXct times: z <- as.zoo(x) time(z) <- as.numeric(time(z) - start(z))
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
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
Gauss once played himself in a zero-sum game and won $50.
-- #11 at http://www.gaussfacts.com
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110605/26b25074/attachment.pl>
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
Gauss once played himself in a zero-sum game and won $50.
-- #11 at http://www.gaussfacts.com
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110607/9663ec2a/attachment.pl>