I have two data tables. The first, DT1, is grouped by ID. It has a Week column that contains an instance of every consecutive week in a large time frame for every ID. It also contains a Units column. It looks something like this simplified version:
ID Week Units
A 1/1/2019 5
A 1/8/2019 9
A 1/15/2019 0
A 1/22/2019 1
B 1/1/2019 1
B 1/8/2019 32
B 1/15/2019 2
B 1/22/2019 6
C 1/1/2019 0
C 1/8/2019 0
C 1/15/2019 8
C 1/22/2019 3
The second table, DF2, has a time range window for every ID. This is represented by a start date column and a stop date column. It looks something like this simplified version:
ID Start Date Stop Date
A 1/1/2019 1/8/2019
B 1/8/2019 1/22/2019
C 1/8/2019 1/15/2019
I would like to calculate the sum of the units column for every date range/buyer combination in DF2. My desired output would be:
ID Start_Date Stop_Date sumUnits
A 1/1/2019 1/8/2019 14
B 1/8/2019 1/22/2019 40
C 1/8/2019 1/15/2019 8
Is there a way to calculate this type of sum in R?
I have referenced the article, In R: how to sum a variable by group between two dates , as well as attempted the interval function and a non-equi join.
DT1[DT2[DT1, sum(x), on = .(Units, Week>= Stop_Date, Week<= Stop_Date),
by = .EACHI], newvar := V1, on = .(Units, Start_Date
=Week)]
It returns the message, "Week" not in i
. How do I solve the problem?