3
打算在數據表中添加雙週數日期列。我有一個工作解決方案,但似乎凌亂。另外,我有這種感覺,滾動連接應該做到這一點,但我不知道如何。是否有更好的解決方案來創建雙週日期的分組?雙週數據表分檔
# Mock data table
dt <- data.table(value = runif(20), date = seq(as.Date("2015-01-01"), as.Date("2015-01-20"), by = "days"))
# Bi-weekly dates starting with most recent date and working backwards
bidates <- data.table(bi = seq(dt[, max(date)], dt[, min(date)], by = -14))
# Expand out bi-weekly dates to match up with every date in that range
bidates <- bidates[, seq(bi - 13, bi, by = "days"), by = bi]
# Key and merge
setkey(dt, date)
setkey(bidates, V1)
dt[bidates, bi := i.bi]