I came across the following problem today and I am wondering if there is a better way to accomplish what I am trying to do.
Let's suppose I have the following data.table
(just an hourly timestamp):
library(data.table)
tdt <- data.table(Timestamp = seq(as.POSIXct("1980-01-01 00:00:00"), as.POSIXct("2015-01-01 00:00:00"), '1 hour'))
> tdt
Timestamp
1: 1980-01-01 00:00:00
2: 1980-01-01 01:00:00
3: 1980-01-01 02:00:00
4: 1980-01-01 03:00:00
5: 1980-01-01 04:00:00
---
306813: 2014-12-31 20:00:00
306814: 2014-12-31 21:00:00
306815: 2014-12-31 22:00:00
306816: 2014-12-31 23:00:00
306817: 2015-01-01 00:00:00
My goal is to change the minutes of the timestamp to, say, 10 minutes.
I know I can use:
library(lubridate)
minute(tdt$Timestamp) <- 10
but this does not utilize the super fast speed of data table (which I need). On my laptop this took:
> system.time(minute(tdt$Timestamp) <- 10)
user system elapsed
11.29 0.16 11.45
So, my question is: Can we somehow use a replacement function in the data table syntax so that it will do what I want using data.table
's speed? If the answer is no, any other data.table
solution to do this fast, would be acceptable.
If you wonder one of the things I tried is:
tdt[, Timestamp2 := minute(Timestamp) <- 10]
which does not work.
Expected Output (but with data table syntax):
> tdt
Timestamp
1: 1980-01-01 00:10:00
2: 1980-01-01 01:10:00
3: 1980-01-01 02:10:00
4: 1980-01-01 03:10:00
5: 1980-01-01 04:10:00
---
306813: 2014-12-31 20:10:00
306814: 2014-12-31 21:10:00
306815: 2014-12-31 22:10:00
306816: 2014-12-31 23:10:00
306817: 2015-01-01 00:10:00