如何将表示小时的数字从数值格式转换为HMS格式。
例如:
例如:
8.0 -> 08:00:00
0.5 -> 00:30:00
0.25 -> 00:15:00
另一个基本的R选项:
x <- c(8, 0.5, 0.25)
format(as.POSIXct(x * 3600, origin = '1970-01-01', tz = 'UTC'), '%T')
#[1] "08:00:00" "00:30:00" "00:15:00"
将时间转换为秒,使用 lubridate
中的 seconds_to_periods
,并将其转换为 hms
hms::hms(lubridate::seconds_to_period(floor(v1 * 60 *60)))
#08:00:00
#00:30:00
#00:15:00
v1 <- c(8, 0.5, 0.25)
这里有一个基于 R 的选项
h <- x%/%1
m <- floor(x%%1 * 60)
s <- round((x%%1 * 60)%%1*60)
sprintf("%02d:%02d:%02d",h,m,s)
> sprintf("%02d:%02d:%02d",h,m,s)
[1] "08:00:00" "00:30:00" "00:15:00" "03:45:22"
数据
x <- c(8.0,0.5,0.25,3.756)
data.table::as.ITime(x*3600)
。 - Henrikchron::times(x/24)
。 - Henrik