[R] Millisecond TimeStamps

Dirk Eddelbuettel edd at debian.org
Thu Mar 24 21:23:50 CET 2011


On 24 March 2011 at 13:13, 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.  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"
| 
|  
| 
|  
| 
| 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 all already works.  You are simply being tricked by the common issue that
_printed_ representation is not the same as _actual_ representation.

See this:

R> now <- Sys.time()
R> now
[1] "2011-03-24 15:21:25 CDT"
R> options(digits.secs=6)              ## switch to subsecond display
R> now
[1] "2011-03-24 15:21:25.347843 CDT"   ## et voila, milliseconds revealed
R> 

On Windows you get milliseconds, on operating systems with an X you get
microseconds.  All of this is stored as _fractional_ seconds since the epoch
very neatly generalising the POSIX concept of integer seconds since the epoch.

Dirk
 
|  
| 
| Thanks
| 
| Madaliso
| 
| 
| 	[[alternative HTML version deleted]]
| 
| ______________________________________________
| R-help at r-project.org 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.

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com



More information about the R-help mailing list