我如何在 R 中巧妙地找到以前的时间段开始和结束?
How can I in R cleverly find previous time periods start and end?
我想在 R 中找到之前周期的开始和结束。时间段以分钟为单位定义。
喜欢下面的输出。
library(lubridate)
timeNow <- now()
timeNow
# [1] "2019-04-17 11:17:09 CEST"
periodLength <- 60 #minutes
previousPeriodStart(timeNow, periodLength)
# [1] "2019-04-17 10:00:00 CEST"
previousPeriodEnd(timeNow, periodLength)
# [1] "2019-04-17 10:59:59 CEST"
periodLength <- 5 #minutes
previousPeriodStart(timeNow, periodLength)
# [1] "2019-04-17 11:10:00 CEST"
previousPeriodEnd(timeNow, periodLength)
# [1] "2019-04-17 11:14:59 CEST"
如果我有带数据的 ts,我知道如何在 xts 中构建这些函数。
因为我没有这个例子的数据集,所以我不能使用 xts to.period 函数。相反,我只想找到开始和结束。
构造 previousPeriodStart 和 previousPeriodEnd 的最聪明的方法是什么?或者我应该开始用 epoch/unix 次算术?
非常感谢。
--- 编辑 ----
由于接收到一个函数的答案在同一个函数中回答开始和结束,这当然要聪明得多。
是这样的吗?
library(lubridate)
timeNow <- now()
period = 60
floor_date(timeNow, unit="hour")-minutes(period)+minutes( trunc(minute(timeNow)/period)*period)
floor_date(timeNow, unit="hour")-minutes(period)+minutes( trunc(minute(timeNow)/period)*period)+minutes(period)-seconds(1)
period = 5
floor_date(timeNow, unit="hour")-minutes(period)+minutes( trunc(minute(timeNow)/period)*period)
floor_date(timeNow, unit="hour")-minutes(period)+minutes( trunc(minute(timeNow)/period)*period)+minutes(period)-seconds(1)
使用 lubridate
这可以轻松完成:
previousPeriod <- function(timeNow,periodLength)
{
start <- floor_date(timeNow - minutes(60),"hour")
end <- ceiling_date(timeNow - minutes(60),"hour") - seconds(1)
return(c(start=start,end=end))
}
previousPeriod(timeNow,periodLength)
start end
"2019-04-17 10:00:00 UTC" "2019-04-17 10:59:59 UTC"
在使用 floor_date 的其他两个答案的基础上,我改进了最快的答案,以包括除整小时以外的其他分钟间隔的工作代码。
previousPeriods <- function(timeNow, periodLength)
{
periodPhrase <- paste0(periodLength, " mins") # To create periods such as "5 mins"
start <- floor_date(timeNow - minutes(periodLength), periodPhrase)
end <- ceiling_date(timeNow - minutes(periodLength), periodPhrase) - seconds(1)
return(c(start=start,end=end))
}
再次感谢所有快速回答的帮助。
我想在 R 中找到之前周期的开始和结束。时间段以分钟为单位定义。
喜欢下面的输出。
library(lubridate)
timeNow <- now()
timeNow
# [1] "2019-04-17 11:17:09 CEST"
periodLength <- 60 #minutes
previousPeriodStart(timeNow, periodLength)
# [1] "2019-04-17 10:00:00 CEST"
previousPeriodEnd(timeNow, periodLength)
# [1] "2019-04-17 10:59:59 CEST"
periodLength <- 5 #minutes
previousPeriodStart(timeNow, periodLength)
# [1] "2019-04-17 11:10:00 CEST"
previousPeriodEnd(timeNow, periodLength)
# [1] "2019-04-17 11:14:59 CEST"
如果我有带数据的 ts,我知道如何在 xts 中构建这些函数。 因为我没有这个例子的数据集,所以我不能使用 xts to.period 函数。相反,我只想找到开始和结束。
构造 previousPeriodStart 和 previousPeriodEnd 的最聪明的方法是什么?或者我应该开始用 epoch/unix 次算术?
非常感谢。
--- 编辑 ---- 由于接收到一个函数的答案在同一个函数中回答开始和结束,这当然要聪明得多。
是这样的吗?
library(lubridate)
timeNow <- now()
period = 60
floor_date(timeNow, unit="hour")-minutes(period)+minutes( trunc(minute(timeNow)/period)*period)
floor_date(timeNow, unit="hour")-minutes(period)+minutes( trunc(minute(timeNow)/period)*period)+minutes(period)-seconds(1)
period = 5
floor_date(timeNow, unit="hour")-minutes(period)+minutes( trunc(minute(timeNow)/period)*period)
floor_date(timeNow, unit="hour")-minutes(period)+minutes( trunc(minute(timeNow)/period)*period)+minutes(period)-seconds(1)
使用 lubridate
这可以轻松完成:
previousPeriod <- function(timeNow,periodLength)
{
start <- floor_date(timeNow - minutes(60),"hour")
end <- ceiling_date(timeNow - minutes(60),"hour") - seconds(1)
return(c(start=start,end=end))
}
previousPeriod(timeNow,periodLength)
start end
"2019-04-17 10:00:00 UTC" "2019-04-17 10:59:59 UTC"
在使用 floor_date 的其他两个答案的基础上,我改进了最快的答案,以包括除整小时以外的其他分钟间隔的工作代码。
previousPeriods <- function(timeNow, periodLength)
{
periodPhrase <- paste0(periodLength, " mins") # To create periods such as "5 mins"
start <- floor_date(timeNow - minutes(periodLength), periodPhrase)
end <- ceiling_date(timeNow - minutes(periodLength), periodPhrase) - seconds(1)
return(c(start=start,end=end))
}
再次感谢所有快速回答的帮助。