Convert Decimal to HH: MM

I have a decimal number: 0.8541667and I want to convert it to the format HH: the MM, using R. The answer that I'm looking for is: 20:30.

+6
source share
3 answers

Try the following:

R> format(as.POSIXct(Sys.Date() + 0.8541667), "%H:%M", tz="UTC")
[1] "20:30"
R> 

We start with a date - which can be any date, so we use today - and add the desired fractional day.

Then we convert the Date type to a Datetime object.

Finally, we format the time and minute parts of the Datetime object, ensuring that UTC is used for the time zone.

+11
source

Usage chron:

chron::times(0.8541667)
#[1] 20:30:00
+11
source

data.table:

> library(data.table)
> structure(as.integer(0.4305556*60*60*24), class="ITime")
[1] "10:20:00"

; ; ITime. (ITime .)

:

+1
source

Source: https://habr.com/ru/post/1614279/


All Articles