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?
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With