How to parse milliseconds?
49,196
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
Comments
-
signalseeker over 3 years
How do I use
strptime
or any other functions to parse time stamps 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"`
-
signalseeker over 14 yearsThanks, I missed that in the strptime doc. I was looking for a format character and gave up when I did not see one.
-
jkff over 13 yearsIf I could put a memorial in your honor, I would!
-
Pierre D over 11 yearsso would I! The "%OS" bit is awesome.
-
firebush over 8 yearsIs this only in python3 or something? In my python2.7.8: >>> time.strptime(t, "%Y-%m-%d %H:%M:%OS") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/pythons/2.7.8/lib/python2.7/_strptime.py", line 467, in _strptime_time return _strptime(data_string, format)[0] File "/opt/pythons/2.7.8/lib/python2.7/_strptime.py", line 317, in _strptime (bad_directive, format)) ValueError: 'O' is a bad directive in format '%Y-%m-%d %H:%M:%OS'
-
IRTFM about 8 years@firebush: It's in R. It might require "%Y-%m-%d %H:%M:%OS3" on some platforms. The implementation of the "OS" format is labeled as OS-specific on the help pages.