且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在R中将时间从数字格式转换为时间格式

更新时间:2023-12-05 10:58:16

as.POSIXct在将您的号码乘以一天中的秒数后(60 * 60 * 24)

  nTime< -  c(0.38402777777778,0.3847222222222222,0.3854166666666667)
格式(as.POSIXct((nTime)* 86400,origin =1970-01-01,tz =UTC),%H:%M)
## [1]09:1309:1409:15


I read data from an xls file. Apparently, the time is not in the right format. It is as follows (for example)

0.3840277777777778
0.3847222222222222
0.3854166666666667

Indeed, they should be

09:12
09:13
09:13

I don't know how to convert it to the right format. I searched several threads and all of them are about converting the date (with/without time) to the right format.

Can somebody give me any clues?

You can use as.POSIXct after having multiplied your number by the number of seconds in a day (60 * 60 * 24)

nTime <- c(0.3840277777777778, 0.3847222222222222, 0.3854166666666667)
format(as.POSIXct((nTime) * 86400, origin = "1970-01-01", tz = "UTC"), "%H:%M")
## [1] "09:13" "09:14" "09:15"