Home > Software engineering >  How to use lubridate within data.table to manipulate dates?
How to use lubridate within data.table to manipulate dates?

Time:01-18

How do I achieve the desirable outcome (creating a new date column that is a function of another date column) using data.table and lubridate packages?

Here's simple code, which does not work:

library(data.table); library(lubridate);
dt <- data.table(date=ymd( paste0("2022-01-", 1:5) ) ); dt 
         date
       <Date>
1: 2022-01-01
2: 2022-01-02
3: 2022-01-03
4: 2022-01-04
5: 2022-01-05

dt[, `Report date` := ymd(year(date)-5, month(date), day(date))]

Error in `[.data.table`(dt, , `:=`(`Report date`, ymd(year(date) - 5,  : 
  Supplied 15 items to be assigned to 5 items of column 'Report date'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.
In addition: Warning message:
All formats failed to parse. No formats found. 

Thanks!

CodePudding user response:

Another option could simply be:

dt[, `Report date` := date - years(5)]
dt
         date Report date
       <Date>      <Date>
1: 2022-01-01  2017-01-01
2: 2022-01-02  2017-01-02
3: 2022-01-03  2017-01-03
4: 2022-01-04  2017-01-04
5: 2022-01-05  2017-01-05

CodePudding user response:

ymd expects a single string instead of 3 parts, we may need to simply paste

library(data.table)
library(lubridate)
dt[, `Report date` := ymd(paste(year(date)-5, month(date), day(date)))]

Or instead of ymd can also use ISOdate which can take multiple arguments to combine to datetime

dt[, `Report date2` := as.IDate(ISOdate(year(date)-5, month(date), day(date)))]

-output

> dt
         date Report date Report date2
1: 2022-01-01  2017-01-01   2017-01-01
2: 2022-01-02  2017-01-02   2017-01-02
3: 2022-01-03  2017-01-03   2017-01-03
4: 2022-01-04  2017-01-04   2017-01-04
5: 2022-01-05  2017-01-05   2017-01-05
  • Related