Maximal frustration in printing and parsing milliseconds in R

I’ve just spent an absurd amount of time figuring out how to parse millisecond times into R. (It’s standard practice to timestamp tick data with milliseconds timing in a flat data file.) Turns out that there are two problems that I faced. One being that there is a almost-hidden, footnote option in the strptime( ) function (for converting characters to POSIX) in which it describes using format = “%H:%M%OS” instead of “%H:%M:%S” to parse fractional seconds. However, the second, and what’s actually the one that’s wasted much my time, is the fact that R ignores milliseconds in printing by default!

For example, here’s an output for the POSIXct representation of the integer value 1286564400.

“2010-10-08 15:00:00 EDT”

So even though I figured how to represent millisecond times in R a few hours ago, I never knew I did it simply because R is not printing the output that I wanted on screen every time I tried!

After a couple hours of searching, trying, and frustrating, here’s the one-liner command that solved my problem.

> options(digits.secs=3)

which changes the options of the display and thus I can see the following output from the same input as above.

“2010-10-08 15:00:00.344 EDT”

I guess that’s why they say R has a steep learning curve! Yes, it is very powerful and flexible. But there are limited standards and the documentations are all over the place because it’s a mish mash of user-contributed packages!

No related posts.

4 Comments

  1. Shane says:

    While R has a steep curve, it also has an amazing community. You probably could have answered your question immediately if you submitted it on stackoverflow.com (the R tag), to the #rstats tag on twitter, or the numerous R email lists…

  2. Paul says:

    Hi Shane,
    Thanks for the suggestions. I am subscribing to R-SIG-Finance.

    I eventually found my answer on stackoverflow. I use it often for python and java problems. But I haven’t found a good way to search for R-specific issues yet. Google turns up too many unrelated results on “R” most of the time for me.

    I bookmarked your blog since you seem to use R extensively for computational finance. I look forward to learning from you.

    Paul

  3. Shane says:

    Thanks. Don’t get much time for blogging, but am optimistic that I will have more time in the future.

    On stackoverflow, just use the r tag. It’s very active. http://stackoverflow.com/questions/tagged/r

  4. laredlared says:

    Here is the solution :

    > options(digits=13)
    > unclass(Sys.time())

Trackbacks/Pingbacks

  1. A failed experiment with spectral density estimation in R | Quantisan.com - [...] is a technical process to visualize a time series data in the frequency domain. As I continue my struggle ...

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>