Average Number of Units Average Revenue Per Unit Average Delta Units add_rpu_ <- function(DT) { stopifnot(c("revenue", "units") %in% names(DT)) DT[, avg_rev_per_unit := revenue / units] } DT.country_level <- add_rpu_(DT.EU_aggd[TestWk == "Wk1" & store_name == "iTunes" & transac_type_abbr != "DV"][, lapply(.SD, sumn), .SDcols = countCols, keyby=list(transac_type_abbr, country_code)]) DT.country_CDs <- add_rpu_(DT.EU_aggd[TestWk == "Wk1" & store_name == "iTunes" & transac_type_abbr != "DV"][, lapply(.SD, sumn), .SDcols = countCols, keyby=list(transac_type_abbr, country_code, multi_cd)]) DT.country_CDs_Override <- add_rpu_(DT.EU_aggd[TestWk == "Wk1" & store_name == "iTunes" & transac_type_abbr != "DV"][, lapply(.SD, sumn), .SDcols = countCols, keyby=list(transac_type_abbr, country_code, OverridePerc)]) Changes.DT <- printGroup("country_code", "relative_to_default", quiet=TRUE, tmp_DT.ll = tmp_DT.ll.DT)[from_relative_to_default == "Zero Overrides" & to_relative_to_default == "Zero Overrides" & from_country_code == to_country_code] [to_TreatmentWk1 == "No Change (8.99)"] Changes.DT[, country_code := from_country_code] Changes.DA <- printGroup("country_code", "relative_to_default", quiet=TRUE, tmp_DT.ll = tmp_DT.ll.DA)[from_relative_to_default == "Zero Overrides" & to_relative_to_default == "Zero Overrides" & from_country_code == to_country_code] [to_TreatmentWk1 == "No Change (8.99)"] Changes.DA[, country_code := from_country_code] Changes.DT[from_TreatmentWk1 == "Increase +1 (9.99)", list(diff, sig), keyby=country_code] Changes.DA[from_TreatmentWk1 == "Increase +1 (9.99)", list(diff, sig), keyby=country_code] DT.No_OverRides <- add_rpu_(DT.country_CDs_Override[OverridePerc == " 0 %"][, lapply(.SD, sumn), keyby=list(transac_type_abbr, country_code), .SDcols = countCols]) setkeyIfNot(DT.No_OverRides, country_code) DT.No_OverRides[transac_type_abbr == "DT", `:=`( avg_change_09.99 = Changes.DT[from_TreatmentWk1 == "Increase +1 (9.99)", diff, keyby=country_code]$diff / 12 , sigg_09.99 = Changes.DT[from_TreatmentWk1 == "Increase +1 (9.99)", sig, keyby=country_code][["sig"]] , avg_change_10.99 = Changes.DT[from_TreatmentWk1 == "Increase +2 (10.99)", diff, keyby=country_code]$diff / 12 , sigg_10.99 = Changes.DT[from_TreatmentWk1 == "Increase +2 (10.99)", sig, keyby=country_code][["sig"]] ) ] DT.No_OverRides[transac_type_abbr == "DA", `:=`( avg_change_09.99 = Changes.DA[from_TreatmentWk1 == "Increase +1 (9.99)", diff, keyby=country_code]$diff / 12 , sigg_09.99 = Changes.DA[from_TreatmentWk1 == "Increase +1 (9.99)", sig, keyby=country_code][["sig"]] , avg_change_10.99 = Changes.DA[from_TreatmentWk1 == "Increase +2 (10.99)", diff, keyby=country_code]$diff / 12 , sigg_10.99 = Changes.DA[from_TreatmentWk1 == "Increase +2 (10.99)", sig, keyby=country_code][["sig"]] ) ] DT.No_OverRides[, total_change_in_revenue_10.99 := avg_change_10.99 * units * avg_rev_per_unit] DT.No_OverRides[, total_change_in_revenue_09.99 := avg_change_09.99 * units * avg_rev_per_unit] DT.No_OverRides[]