我有一个向量,其中包含最近半小时的时间
x <- c(30,200,2200,2300)
我需要把它转换成
output <- c(00:30,02:00,22:00,23:00).
我无法转换少于 4 位的值。
请建议。 strptime()
, as.Date()
为第一个元素抛出 NA
。
我试过下面的代码,但没有用。请建议
代码:
x <- c(30,200,2200,2300)
output <- format(strptime(x,"%H%M"),"%H:%M")
output
#[1] NA "20:00" "22:00" "23:00"
最佳答案
您可以使用 sprintf
添加前导零。
x <- c(30, 200, 2200, 2300)
format(strptime(sprintf("%04d", x), format="%H%M"), format="%H:%M")
# [1] "00:30" "02:00" "22:00" "23:00"
关于r - 如何根据位数将数值转换为小时和分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56950395/