时间戳中的小时数 (POSIXct) 到 R 中某个值的最接近倍数

Round hour in timestamp (POSIXct) to the nearest multiple of a certain value in R

我有一个 date/time 值 (POSIXct),我想将 "hour" 值四舍五入为三的倍数(00:00、03:00、6:00 , 9:00, 12:00...).

到目前为止,我已经将小时提取为整数,相应地四舍五入并将其插入回 POSIXct 格式。有没有更快更优雅的方式?到目前为止,这是我的代码:

timestamp <- as.POSIXct("2015-10-14 14:00")
timestamp.h <- as.numeric(format(timestamp, "%H")) + as.numeric(format(timestamp, "%M"))/60
timestamp.h.up <- ceiling(timestamp.h/3)*3
timestamp.up <- as.POSIXct(paste(format(timestamp, "%Y-%m-%d")," ",timestamp.h.up,":00", sep=""))

转换到 POSIXlt 和返回到 POSIXct 的速度要快一点:

f0 <- function( timestamp )
{
  timestamp.h <- as.numeric(format(timestamp, "%H")) + as.numeric(format(timestamp, "%M"))/60
  timestamp.h.up <- ceiling(timestamp.h/3)*3
  timestamp.up <- as.POSIXct(paste(format(timestamp, "%Y-%m-%d")," ",timestamp.h.up,":00", sep=""))
}

f1 <- function( t )
{
  x <- as.POSIXlt(t)
  x[["hour"]] <- 3*ceiling((60*x[["hour"]]+x[["min"]])/180)
  x[["min"]] <- 0

  return(as.POSIXct(x))
}

.

> timestamp <- as.POSIXct("2015-10-14 15:03")

> system.time(
+   for ( i in 1:10000) { t0 <- f0(timestamp) }
+   )
   user  system elapsed 
  16.94    0.00   17.19 

> system.time(
+   for ( i in 1:10000) { t1 <- f1(timestamp) }
+ )
   user  system elapsed 
   2.56    0.00    2.56 

> t0
[1] "2015-10-14 18:00:00 CEST"

> t1
[1] "2015-10-14 18:00:00 CEST"

.

> timestamp <- as.POSIXct("2015-10-14 14:00")

> system.time(
+   for ( i in 1:10000) { t0 <- f0(timestamp) }
+   )
   user  system elapsed 
  14.00    0.00   14.21 

> system.time(
+   for ( i in 1:10000) { t1 <- f1(timestamp) }
+ )
   user  system elapsed 
   1.25    0.00    1.24 

> t0
[1] "2015-10-14 15:00:00 CEST"

> t1
[1] "2015-10-14 15:00:00 CEST"
>