views:

395

answers:

2

How do I use strptime or any other functions to parse timestamps with milliseconds in R?

> time[1]
[1] "2010-01-15 13:55:23.975"
> strptime(time[1], format="%Y-%m-%d %H:%M:%S.%f")
[1] NA
> strptime(time[1], format="%Y-%m-%d %H:%M:%S")
[1] "2010-01-15 13:55:23"`
+4  A: 

Courtesy of the ?strptime help file (with the example changed to your value):

 z <- strptime("2010-01-15 13:55:23.975", "%Y-%m-%d %H:%M:%OS")
 z # prints without fractional seconds
 op <- options(digits.secs=3)
 z
 options(op) #reset options
Aniko
Thanks, I missed that in the strptime doc. I was looking for a format character and gave up when I did not see one.
signalseeker
If I could put a memorial in your honor, I would!
jkff
+1  A: 

You can also use strptime(time[1], "%OSn") where 0 <= n <= 6, without having to set digits.secs.

darvids0n