按条件生成一系列时间戳和 R 中的相关结果

Generate a sequence of timestamps by condition and an associated outcome in R

我想获得从“2020-08-31 09:00:00”开始到“2020-10-30 21:00:00”结束的时间戳序列,精确到秒。我想将一个结果变量与其相关联,这样每 30 分钟的结果为 100,接下来的 5 分钟为 1000,然后接下来的 30 分钟为 100,依此类推。系统的值为 30 分钟 100,接下来的 5 分钟为 1000,然后再次为 30 分钟 100。

提前致谢

#Example OUTPUT

 TIMESTAMP                            OUTCOME
'2020-08-31 09:00:00'                 100   
'2020-08-31 09:30:00'                 1000
'2020-08-31 09:35:00'                 100
'2020-08-31 10:05:00'                 1000  
'2020-08-31 10:10:00'                 100  
'2020-08-31 10:40:00'                 1000

非常感谢

我们可以用 seq

创建 DateTime 向量
library(lubridate)
start <- as.POSIXct('2020-08-31 09:00:00')
timestamp <- c(start, start + minutes(cumsum(rep(c(30, 5), 2500))))
timestamp <- timestamp[timestamp <= '2020-10-30 21:00:00']
out <- data.frame(TIMESTAMP = timestamp,
    OUTCOME = rep(c(100, 1000), length.out = length(timestamp)))
head(out)
#            TIMESTAMP OUTCOME
#1 2020-08-31 09:00:00     100
#2 2020-08-31 09:30:00    1000
#3 2020-08-31 09:35:00     100
#4 2020-08-31 10:05:00    1000
#5 2020-08-31 10:10:00     100
#6 2020-08-31 10:40:00    1000