as.POSIXct show milliseconds with format

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

as.POSIXct show milliseconds with format

rivercode
Hi,

Trying to create a POSIXct index for an xts object that will display the POSIXct index as HH:MM:SS.MMM.

First of all, I am trying to get the as.POSIXct to work with format...

> as.POSIXct(paste("2011-03-02 09:00:00.000", sep=""), tz="EST", format="%H:%M:%OS3")
[1] NA

Why is this returning NA ?

I can get Hours and Minutes...but only with the format as %H %M.

> as.POSIXct(paste("2011-03-02 09:00:00.000", sep=""), tz="EST", format="%H %M")
[1] "2011-03-02 20:11:00 EST"

BUT if I do it with format="%H:%M" I also get an NA:
> as.POSIXct(paste("2011-03-02 09:00:00.000", sep=""), tz="EST", format="%H:%M")
[1] NA

What am I not understanding ?

Is it possible to create a POSIXct index for xts (or zoo) that will display (eg. with head(my_xts_object) ) the index in format HH:MM:SS.MMM so I can see the milliseconds.

Thanks,
Chris


Reply | Threaded
Open this post in threaded view
|

Re: as.POSIXct show milliseconds with format

Gabor Grothendieck
On Wed, Mar 2, 2011 at 11:04 PM, rivercode <[hidden email]> wrote:

> Hi,
>
> Trying to create a POSIXct index for an xts object that will display the
> POSIXct index as HH:MM:SS.MMM.
>
> First of all, I am trying to get the as.POSIXct to work with format...
>
>> as.POSIXct(paste("2011-03-02 09:00:00.000", sep=""), tz="EST",
>> format="%H:%M:%OS3")
> [1] NA
>
> Why is this returning NA ?
>
> I can get Hours and Minutes...but only with the format as %H %M.
>
>> as.POSIXct(paste("2011-03-02 09:00:00.000", sep=""), tz="EST", format="%H
>> %M")
> [1] "2011-03-02 20:11:00 EST"
>
> BUT if I do it with format="%H:%M" I also get an NA:
>> as.POSIXct(paste("2011-03-02 09:00:00.000", sep=""), tz="EST",
>> format="%H:%M")
> [1] NA
>
> What am I not understanding ?
>
> Is it possible to create a POSIXct index for xts (or zoo) that will display
> (eg. with head(my_xts_object) ) the index in format HH:MM:SS.MMM so I can
> see the milliseconds.
>

options(digits.secs = 3)

will cause 3 digits to be displayed.

Another thing you could do with zoo is that you could define your own
class that displays any way you like.  Here we have defined just
enough methods of a "mytime" class. This takes advantage of the fact
that zoo does not work with hard coded index classes but rather any
index class with sufficient methods will work with zoo:


library(zoo)

as.mytime <- function(x, ...) UseMethod("as.mytime")

as.mytime.character <- function(x, ...) {
        x <- as.POSIXct(paste("1970-01-01", x), ...)
        structure(x, class = c("mytime", class(x)))
}

as.mytime.POSIXt <- function(x, ...) {
        structure(x, class = c("mytime", setdiff(class(x), "mytime")))
}

as.character.mytime <-
format.mytime <- function(x, format = "%H:%M:%OS3", ...) {
        format.POSIXct(x, format = format, ...)
}

print.mytime <- function(x, ...) {
        print(format(x), ...)
}

Ops.mytime <- function (e1, e2) {
        as.mytime(NextMethod(.Generic))
}

z <- zooreg(1:15, start = as.mytime("13:14:15.100"), frequency = 10)
head(z)

The last line produces:

> head(z)
13:14:15.100 13:14:15.200 13:14:15.300 13:14:15.400 13:14:15.500 13:14:15.600
           1            2            3            4            5            6

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.