我希望按任意精度级别对 R 中的日期进行分组。
将其精确到最接近的小时或分钟非常简单,例如使用润滑
:
library(lubridate)
nearest_hour = floor_date(now(), 'hour')
然后,您可以将此类日期的列表分组,例如来自 plyr
的简单总结
ddply
。
我想做的是以任意精度舍入日期,例如精确到 15 分钟或每 3 小时:
nearest_three_hours = floor_date(now(), '3 hours')
http://r.789695.n4.nabble.com/Truncating-dates-and-other-date-time-manipulations-td866901.html 有关于此类事情的讨论。但除了削减日期之外,似乎没有任何解决方案。
最佳答案
有点晚了,我没有代表发表评论,但正如 Selva 提到的,lubridate 现在具有此功能:
library(lubridate)
round_date(now(), '3 hours')
floor_date(now(), '3 hours')
ceiling_date(now(), '3 hours')
关于将 R 中的日期舍入为任意分钟/小时的精度级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16803867/