我想创建一个从00:00:00到00:38:24的时间序列,相隔1/32秒。这意味着会重复多次,但这并不重要。
我试过了:
require(lubridate)
#Convert 00:38:24 to seconds
timeEnd<-period_to_seconds(hms("00:38:24"))
>timeEnd
[1] 2304
#Total number of elements in sequence
timeEnd*32Hz=73728 elements
#Create a sequence from 0 to timeEnd in 1/32 steps
t<-seq(0,timeEnd,by=1/32) #This bit is wrong! makes 73729 elements
#Convert to 00:00:00 format
sprintf('%02d:%02d:%02d', seconds_to_period(t)@hour, minute(seconds_to_period(t)), second(seconds_to_period(t)))
>t[1:5]
[1] "00:00:00" "00:00:00" "00:00:00" "00:00:00" "00:00:00"
答案 0 :(得分:1)
我不知道这是否是一个更好的解决方案;但是只使用base和stringi并使用函数作为包装器以获得更大的灵活性。时间值可以作为分数或其他任何方式作为字符和时间增量传递。
f <- function(start_time = "00:00:00", end_time = "00:38:24",
time_inc = 1/32, ...){
options(digits.secs=4)
tl <- lapply(list(start_time, end_time), function(i){
as.POSIXct(i, format = "%H:%M:%OS", tz = "EST")
})
as_seq <- seq(tl[[1]], tl[[2]], by = time_inc)
stringi::stri_datetime_format(as_seq, format = "HH:mm:ss:SSSS")
}
time.seq <- f()
> head(time.seq)
[1] "00:00:00:0000" "00:00:00:0310" "00:00:00:0620" "00:00:00:0930" "00:00:00:1250" "00:00:00:1560"
> tail(time.seq)
[1] "00:38:23:8430" "00:38:23:8750" "00:38:23:9060" "00:38:23:9370" "00:38:23:9680" "00:38:24:0000"