lounytoon
lounytoon

Reputation: 21

Convert a decimal number to HH:MM:SS in R

I have a series of decimal numbers (marathon race split times): 64.90, etc., and I want to convert it into HH:MM:SS format using R so that I can us the result to do time math. The answer I am looking for is: 1:04:54.

chron doesn't seem to be doing what I'm expecting it to do.

chron::times(64.90) Time in days: [1] 64.9

First time on this site, so be kind. Thanks.

Upvotes: 1

Views: 3371

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 269654

chron times are measured in days and since you apparently have minutes divide the input by the number of minutes in a day:

library(chron)

times(64.90 / (24 * 60))
## [1] 01:04:54

Upvotes: 3

vsb
vsb

Reputation: 428

library(hms)
as.hms(64.90*60)

output

01:04:54

Upvotes: 2

moodymudskipper
moodymudskipper

Reputation: 47320

You could try lubridate::seconds_to_period

library(lubridate)
seconds_to_period(64.90)
[1] "1M 4.90000000000001S"

Upvotes: 2

Related Questions