r 中是否有一种方法可以在不超出内存的情况下舍入日期?

Is there a method to round date without memory exceed in r?

我正在处理一个数据集,我将 posixct 日期舍入到具有 8800 万行的 data.table 一列中最接近的小时。

我用的是圆形。 Date by base 和 round_date by lubridate,它们都超出了内存,所以无法完成。最后,我将数据集分成 4 等份,四舍五入必要的列,然后绑定回来。

有没有内存效率更高的日期四舍五入的好方法

提前致谢

这应该工作得很好而且很快

#sample data
library( data.table )
n = 1000000
set.seed(123)
DT <- data.table( id = 1:n, 
                  timestamp = sample(seq(as.POSIXct('2017/01/01'), as.POSIXct('2020/05/01'), by="5 mins"), replace = TRUE, n) )

#split timestamp to iDate and iTime
DT[, c("date", "time") := IDateTime( timestamp ) ]
#round the iTime
DT[, time_rounded := round( time, units = "hour" )]
#convert iDate and rounded iTime back to posixct (add timezone if needed)
DT[, timestamp_rounded := as.POSIXct( time_rounded, date = date ) ]

可能的问题:四舍五入到 00:00 第二天...您应该对此进行测试并根据需要调整日期...