简体   繁体   中英

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM