0

I have a numeric column called "hh" with hours in such format:

hh
14.5
15.0
15.5

... representing a whole day I want to format as hour such as:

14:30
15:00
15:30

I tried

format(as.POSIXct(Sys.Date()+df$HH), "%H:%M", tz="UTC", origin="UTC") 

and did not worked. Any suggestions?

loki
  • 9,816
  • 7
  • 56
  • 82
mnd
  • 7
  • 2
  • 1
    Thank you for the reference! Wasn't exactly what I was looking for, but it was good. I save it this solution with a star anyway because I'm always facing problems like this. Thank you again! – mnd Aug 01 '17 at 19:49

1 Answers1

0

Because you ask about how to format the number, here's a solution that outputs the result as a string with your desired formatting.

library(stringr)
paste(hh %/% 1, str_pad(60 * (hh - (hh %/% 1)), width = 2, pad = 0), sep = ":")

If you want it saved as a POSIX object you should refer to what @loki posted in comments.

Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40