Add months to IDate column of data.table
07:29 19 Mar 2013

I have been using data.table for practically everything I was using data.frames for, as it is much, much faster on big in-memory data (several million rows). However, I'm not quite sure how to add days or months to an IDate column without using apply (which is very slow).

A minimal example:

dates = c("2003-01-01", "2003-02-01", "2003-03-01", "2003-06-01", "2003-12-01", 
          "2003-04-01", "2003-05-01", "2003-07-01", "2003-09-01", "2003-08-01")
dt = data.table(idate1=as.IDate(dates))

Now, let's say I want to create a column with dates 6 months ahead. Normally, for a single IDate, I would do this:

seq(dt$idate1[1],by="6 months",length=2)[2]

But this won't work as from= must be of length 1:

dt[,idate2:=seq(idate1,by="6 months",length=2)[2]]

Is there an efficient way of doing it to create column idate2 in dt?

Thanks a lot, RR

r datetime data.table