I have several such data:
sample.data <- rbind(data.table(start.date=seq(from=as.Date("2010-01-01"), to=as.Date("2014-12-01"), by="quarter"),
Group=c("A","B","C","D"), rnorm(20, 5)),
data.table(start.date=seq(from=as.Date("2010-01-01"), to=as.Date("2014-12-01"), by="quarter"),
Group=c("A","B","C","D"), rnorm(20, 3))
)
I would like to create a column end.datethat will be equal to the next early value start.datefor each group.
So, for example, the first start.datefor Group==A- 2010-01-01. The next earliest start.datefor Group==A- 2011-01-01. Thus, the final result should look like this if sorted by Group:
start.date Group end.date
2010-01-01 A 2011-01-01
2010-01-01 A 2011-01-01
2011-01-01 A 2012-01-01
2011-01-01 A 2012-01-01
2012-01-01 A 2013-01-01
2012-01-01 A 2013-01-01
2013-01-01 A 2014-01-01
2013-01-01 A 2014-01-01
2014-01-01 A NA
2014-01-01 A NA
2010-04-01 B 2011-04-01
2010-04-01 B 2011-04-01
2011-04-01 B 2012-04-01
2011-04-01 B 2012-04-01
Etc. Ideally, I would like to do this by reference, for example
sample.data[, end.date := EXPRESSION]
but I don’t understand where to start. Thanks for any help.