Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to express a decimal time in HH:MM

Tags:

time

format

r

I have a time in decimal format: 22,13 in hours and I would like to express this in R as HH:MM. I mean, 22:08 (depreciating seconds)

Anyone can help me?

like image 919
Miguel Santos Avatar asked Sep 12 '25 02:09

Miguel Santos


1 Answers

The syntax %02.0f is to round the value to an integer and make it always be 2 units. Therefore, 7.8 minutes will be converted to character "08".

x <- c("22,13", "9,25", "7,8")

sprintf("%02d:%02.0f",
        as.numeric(sub(",\\d+", "", x)),
        as.numeric(sub("\\d+,", "0.", x)) * 60)

# [1] "22:08" "09:15" "07:48"
like image 133
Darren Tsai Avatar answered Sep 13 '25 15:09

Darren Tsai