We want to complete a DT1 table, by retrieving the sum of scores contained in another DT2 table. By adding to DT1 counters A and B from DT2, making sure that date of DT1 is between the start and end date of DT2. How is this achievable with data table?
Initial data
Table DT1
Date City Squad
2022/1/5 NY a
2022/1/6 NY b
2022/1/9 LA b
2022/1/7 NY a
2022/1/5 NY a
Table DT2
Date_start Date_end City Squad Counter_A Counter_B
2022/1/1 2022/1/11 NY a 1 1
2022/1/2 2022/1/5 NY a 2 3
2022/1/1 2022/1/8 LA b 2 1
2022/1/1 2022/1/8 NY b 1 3
Expected result
Date City Squad Counter_A Counter_B
2022/1/5 NY a 3 4
2022/1/6 NY b 1 3
2022/1/9 LA b 0 0
2022/1/7 NY a 1 1
Init data code
require(data.table)
DT1 <- data.table(
work_day = c("2022/1/5","2022/1/6","2022/1/9","2022/1/7","2022/1/7","2022/1/3"),
city= c("NY", "NY","LA", "NY", "NY", "NY"),
squad=c("a","b","b","a","a","a")
)
DT2 <- data.table(
date_start = c("2022/1/1","2022/1/2","2022/1/1","2022/1/1"),
date_end = c("2022/1/11","2022/1/5","2022/1/8","2022/1/8"),
city= c("NY","NY", "LA", "NY"),
squad=c("a","a","b","b"),
count_A=c(1,2,2,1),
count_B=c(1,3,1,3)
)
Code attempt
I want to do in data table something like the following code in dplyr:
if(DT1$city == DT2$city & DT1$squad == DT2$squad &
DT1$date %in% seq(DT2$date_start,DT2$date_end))
{
DT1$counter_A=DT2$counter_A
DT1$counter_B=DT2$counter_B
} else {
"Nothing"
}