Not the best title... but I'm trying to track tasks outstanding using weekly data exports. I want to use the total task count as of the minimum date (e.g. Jan 1st) of each year as a variable to return percent remaining (proportion) each week throughout that year. See example:
dt <- data.table(id.date=as.Date(c(rep('2019-01-01',80),rep('2019-01-08',60),rep('2019-01-15',40),
rep('2019-01-22',40),rep('2020-01-01',70),rep('2020-01-08',50),
rep('2020-01-15',40),rep('2020-01-29',20))),
task.type=rep('taskA'))[order(id.date)][,year:=year(id.date)]
# 2 sample records per weekly report
dt[,.SD[sample(.N,min(2,.N))],by=id.date]
id.date task.type year
<date> <chr> <int>
2019-01-01 taskA 2019
2019-01-01 taskA 2019
2019-01-08 taskA 2019
2019-01-08 taskA 2019
2019-01-15 taskA 2019
2019-01-15 taskA 2019
2019-01-22 taskA 2019
2019-01-22 taskA 2019
2020-01-01 taskA 2020
2020-01-01 taskA 2020
2020-01-08 taskA 2020
2020-01-08 taskA 2020
2020-01-15 taskA 2020
2020-01-15 taskA 2020
2020-01-29 taskA 2020
2020-01-29 taskA 2020
And the desired output:
# for weekly reports in 2019, 80 tasks would be used for all reports in 2019
# and for 2020, 70 would be used.
id.date N pct.rem
<date> <int> <dbl>
2019-01-01 80 1.0000000
2019-01-08 60 0.7500000
2019-01-15 40 0.5000000
2019-01-22 40 0.5000000
2020-01-01 70 1.0000000
2020-01-08 50 0.7142857
2020-01-15 40 0.5714286
2020-01-29 20 0.2857143
I've made attempts reworking some of the answers from this SO discussion here. But no success. My guess is that I need to utilize .SD or .EACHI is some manner but I'm just starting out with data.table.
Any guidance here is much appreciated. Let me know if more clarity would be helpful. Thanks.