Example of my data
mydata=structure(list(generated_id = c(1003477323030100, 1003477323030100,
1003477323030100, 1003477323030100, 1003477323030100, 1003477323030100,
1003477323030100, 1003477323030100, 1003477323030100, 1003477323030100,
1003477323030100, 1003477323030100, 1003477323030100, 1003477323030100,
1003477323030100, 1003477323030100, 1003477323030100), campaign_id.x = c(23843069854050700,
23843069854050700, 23843069854050700, 23843069854050700, 23843069854050700,
23843069854050700, 23843069854050700, 23843069854050700, 23843069854050700,
23843069854050700, 23843069854050700, 23843069854050700, 23843069854050700,
23843069854050700, 23843069854050700, 23843069854050700, 23843069854050700
), campaign_id.y = c(23843069854050700, 23843069854050700, 23843069854050700,
23843069854050700, 23843069854050700, 23843069854050700, 23843069854050700,
23843069854050700, 23843069854050700, 23843069854050700, 23843069854050700,
23843069854050700, 23843069854050700, 23843069854050700, 23843069854050700,
23843069854050700, 23843069854050700), spent = c(73.5, 73.5,
73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 29.74,
29.74, 29.74, 29.74, 29.74, 29.74), date = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("04.10.2018",
"26.09.2018"), class = "factor"), realpurchase_cash = c(1.49,
1.49, 1.49, 1.49, 1.49, 1.49, 1.49, 1.49, 1.49, 1.49, 1.49, 1.49,
1.49, 1.49, 1.49, 1.49, 1.49), utc_time.y = structure(c(5L, 8L,
2L, 1L, 4L, 4L, 9L, 10L, 6L, 3L, 7L, 5L, 8L, 2L, 1L, 4L, 4L), .Label = c("01.10.2018 22:26",
"05.10.2018 22:34", "05.10.2018 22:35", "06.10.2018 13:43", "07.10.2018 15:55",
"30.09.2018 11:22", "30.09.2018 11:23", "30.09.2018 12:00", "30.09.2018 12:23",
"30.09.2018 18:12"), class = "factor")), .Names = c("generated_id",
"campaign_id.x", "campaign_id.y", "spent", "date", "realpurchase_cash",
"utc_time.y"), class = "data.frame", row.names = c(NA, -17L))
I need to restructure as follows:
if for the group
generated_id +capmaing_id.x+campaing_id.y
the aggregated up to 90 days value ofrealpurchase_cash
is greater than the aggregated up to 90 days value of spent, then the whole group is assign to 1, otherwise 0. To aggregate spent by sum by months , it is column date, but to aggregaterealpurchase_cash
by sum by months , it is columnutc_time.y
so aggregated sum for spent 984 for 2 months, and aggregated sum for realpurchase_cash=25, so flag=0
each group have data up to 90 days no more.
I.E.output
i decided use sqldf solution, cause i work with sql i do so
a1s <- sqldf("
select
generated_id,
[capmaing_id.x],
[campaign_id.y],
spent,
[date],
[utc_time.y],
realpurchase_cash,
--SUM(spent) over (partition by generated_id,[capmaing_id.x],[campaign_id.y]) as sum_spent,
--SUM(realpurchase_cash) over (partition by generated_id,[capmaing_id.x],[campaign_id.y]) as sum_realpurchase_cash
case when SUM(realpurchase_cash) over (partition by generated_id,[capmaing_id.x],[campaign_id.y])>SUM(spent) over (partition by generated_id,[capmaing_id.x],[campaign_id.y]) then 1 else 0 end as flag
from newest3
")
and get the error
Error in result_create(conn@ptr, statement) : near "over": syntax error
How to do correct?