将R中的日期舍入到任意精度级别

phi*_*ams 11 datetime r rounding

我希望以R的任意精度对R中的日期进行分组.

使用例如lubridate:到最近的小时或分钟这样做是非常简单的:

library(lubridate)
nearest_hour = floor_date(now(), 'hour')
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用例如简单的summarise ddplyfrom 来分组此类日期的列表plyr.

我想做的是以任意精度舍入日期,例如最近的15分钟或每3小时:

nearest_three_hours = floor_date(now(), '3 hours')
Run Code Online (Sandbox Code Playgroud)

http://r.789695.n4.nabble.com/Truncating-dates-and-other-date-time-manipulations-td866901.html上有关于此类事情的讨论,但在切割日期之外,似乎没有有任何决议.

任何帮助赞赏!谢谢.

小智 7

有点晚了,我没有代表发表评论,但正如塞尔瓦提到的,lubridate 现在有这个功能:

library(lubridate)
round_date(now(), '3 hours')
floor_date(now(), '3 hours')
ceiling_date(now(), '3 hours')
Run Code Online (Sandbox Code Playgroud)


use*_*028 5

您可以尝试这个,仍然基于lubridate

library(lubridate)
round_minute<-function(x,precision){
  m<-minute(x)+second(x)/60
  m.r<- round(m/precision)*precision
  minute(x)<-m.r
  second(x)<-0
  x
}

round_minute(ymd_hms(c("2013-06-03 22:53:00","2013-05-03 12:18:00","2013-05-03 00:10:00")),15)

> "2013-06-03 23:00:00 UTC" "2013-05-03 12:15:00 UTC" "2013-05-03 00:15:00 UTC"
Run Code Online (Sandbox Code Playgroud)

代码处理所有棘手的情况,多亏了lubridate.当然,此功能仅适用于以分钟表示的精度,但您可以轻松地将其扩展到其他单位,甚至可以创建一个通用函数,如果您真的需要它.


gim*_*ney 5

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
}
Run Code Online (Sandbox Code Playgroud)

  • lubridate 现在在它的 round_date 函数中有这个功能,请改用它 (2认同)

Sel*_*lva 5

lubridate现在有一个更通用的round_date()功能。

lubridate::round_date(date, "5 mins")
lubridate::round_date(date, "2 hours")
Run Code Online (Sandbox Code Playgroud)