How to convert numeric values to time without the date?

后端 未结 1 1301

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:4

相关标签:
1条回答
  • 2020-12-07 06:36

    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)
    
    0 讨论(0)
提交回复
热议问题