lubridate已經樓層到最近的原子單位。爲了達到最接近的15分鐘,我認爲這是你想要做的(不是圓的),你只需要通過findInterval和一組定義的斷點映射到正確的範圍。試試這個floor_time,它在功能上等同於floor_date,但是允許你指定一個變量的秒數,分鐘或小時數。
floor_time <- function(x, k = 1, unit = c("second", "minute", "hour", "day",
"week", "month", "year")) {
require(lubridate)
nmax <- NULL
switch(unit, second = {nmax <- 60},
minute = {nmax <- 60},
hour = {nmax <- 24})
cuts <- seq(from = 0, to = nmax - 1, by = k)
new <- switch(unit,
second = update(x, seconds = cuts[findInterval(second(x), cuts)]),
minute = update(x, minutes = cuts[findInterval(minute(x), cuts)],
seconds = 0),
hour = update(x, hours = cuts[findInterval(hour(x), cuts)],
minutes = 0, seconds = 0),
day = update(x, hours = 0, minutes = 0, seconds = 0),
week = update(x, wdays = 1, hours = 0, minutes = 0, seconds = 0),
month = update(x, mdays = 1, hours = 0, minutes = 0, seconds = 0),
year = update(x, ydays = 1, hours = 0, minutes = 0, seconds = 0))
new
}
如果你有時間的矢量從,然後於子集你可以在[xts](http://cran.project.org/package=xts)中使用'endpoints'函數:'library(xts); x < - .POSIXct(0)+1:10 * 60 * 60 ; x [終點(x,「小時」,3)]' – GSee