Azhagiri
Azhagiri

Reputation: 1

How to convert numeric values to time without the date?

I want convert numeric values to time without the date for the data like 1215,1423,1544,1100,0645,1324 in R. These data has to read like 12:15,14:23,15:44. I was trying as.POSIXct.

Upvotes: 0

Views: 339

Answers (1)

akrun
akrun

Reputation: 886938

We can use strptime with format

format(strptime(sprintf("%04d", v1), "%H%M"), "%H:%M")

The above output is character class, but if we needed a times class, then we can use times from chron on a "HH:MM:SS" format created with sub or from the above code

library(chron)
times(sub("(.{2})(.{2})","\\1:\\2:", sprintf("%04d00", v1)))
#[1] 12:15:00 14:23:00 15:44:00 11:00:00 06:45:00 13:24:00

Or

times(format(strptime(sprintf("%04d", v1), "%H%M"), "%H:%M:%S"))

data

v1 <- c( 1215,1423,1544,1100,0645,1324)

Upvotes: 2

Related Questions