我有一个日期/时间值(POSIXct)我要围绕"小时"值为三的倍数(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=""))
答案 0 :(得分:1)
转换为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"
>