[R] Millisecond TimeStamps

David Winsemius dwinsemius at comcast.net
Thu Mar 24 21:35:05 CET 2011


On Mar 24, 2011, at 1:13 PM, Madaliso Mulaisho wrote:

> I am wondering if there is a good way to work with data that is  
> indexed in
> time, via timestamps with a resolution in milliseconds.  As I  
> understand it,
> the POSIX classes have a resolution i n terms of seconds, and will not
> process fractional seconds from a string.  Is this correct.

No.

>  I realize that
> this may be a little unclear.  Here is what I am trying to do:
>
> A data frame with a time series and a price series, there the time  
> series is
> of the form:
>
> "2009-09-30 10:00:00.543"
>
> I ultimately like to create an xts object out of this data frame, or  
> some
> other object where I can easily work with times (find out how much  
> time has
> elapsed, between entries, etc).
>
> Using, for example, the code:
>
> as.POSIXct("2009-09-30 10:00:00.543", "%Y-%m-%d %H:%M:%S", tz="UTC")
>
> I find this returned:
>
>                "2009-09-30 10:00:00 UTC"
>
>> From various experiments similar to the above, and from the forums,  
>> it first
> seemed like POSIX could not process millisecond time stamps.   
> However, when
> I call:
>
>                Sys.time()
>
> I get a POSIX object that has millisecond timestamps:
>
>                "2011-03-24 13:11:52.79 EDT"

By default the print method for POSIXt vectors does not display the  
subsecond values but they are not removed.

 > as.POSIXct("2009-09-30 10:00:00.543")
[1] "2009-09-30 10:00:00 EDT"

 > format(as.POSIXct("2009-09-30 10:00:00.543"), format="%H:%M:%OS3")
[1] "10:00:00.543"

>
> This has made me confused.  Does anyone know a way to go from a string
> containing time data to a POSIX object with millisecond timestamps?

It is all explained in the help files.

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list