CER
CER

Reputation: 27

convert string to time in r

I have an array of time strings, for example 115521.45 which corresponds to 11:55:21.45 in terms of an actual clock.

I have another array of time strings in the standard format (HH:MM:SS.0) and I need to compare the two.

I can't find any way to convert the original time format into something useable.

I've tried using strptime but all it does is add a date (the wrong date) and get rid of time decimal places. I don't care about the date and I need the decimal places:

for example

t <- strptime(105748.35, '%H%M%OS') =  ... 10:57:48

using %OSn (n = 1,2 etc) gives NA.

Alternatively, is there a way to convert a time such as 10:57:48 to 105748?

Upvotes: 0

Views: 325

Answers (1)

canary_in_the_data_mine
canary_in_the_data_mine

Reputation: 2393

Set the options to allow digits in seconds, and then add the date you wish before converting (so that the start date is meaningful).

options(digits.secs=3)

strptime(paste0('2013-01-01 ',105748.35), '%Y-%M-%d %H%M%OS')

Upvotes: 1

Related Questions