I would like to create a two flags
order.last.30.days
order.anytime.in.past
on the following data.
library(data.table)
library(lubridate)
my.data <- data.table(
supplier = c("a","a","a","a","a","a","b","b","b","b","b","b"),
date = rep(c("2017-06-01","2017-03-01","2017-02-01","2017-01-12",
"2017-05-01","2017-04-01"), 2),
order = c(1,0,0,1,1,0,0,1,0,0,1,0)
)
my.data[,date := ymd(date)]
setorder(my.data, supplier, date)
my.data[, prev.date := shift(date, type = c("lag")),
by = .(supplier)]
my.data[, days.btw.dates := time_length( interval(prev.date, date),
unit = "days")]
How can I do this using shift in the data.table package?