Abhinav Rawat Abhinav Rawat - 2 years ago 122
R Question

How to prevent R from rounding seconds

I have a csv file containing timed data... now when i read it using

it reads the time col as character--->

"19:56:05.938836" "19:56:06.269024" "19:56:06.868525" "19:56:15.080690" "19:56:15.422007" "19:56:16.132036"

now to convert it into a time object i am using
, which gives time object as-->

19:56:06 19:56:06 19:56:07 19:56:15 19:56:15 19:56:16

Clearly it is rounding the time... which i don't want, as i need millisecond resolution.
Is there a way to convert that char time vector to time object without any rounding off?


so i want it to be(I need)-

19:56:05 19:56:06 19:56:06 19:56:15 19:56:15 19:56:16

as time object so that I can compare hours, mins etc...

Answer Source

You can strip off the 'milliseconds' from your string and convert that to a 'time' object as follows:

> chron::times(sub("\\..*", "", "19:56:05.938836"))
[1] 19:56:05
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download