我的数据示例
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))
我需要进行如下重组:
如果对于组
generated_id +capmaing_id.x+campaing_id.y
而言,realpurchase_cash
的最长90天总值大于所花费的最长90天的总值,则将整个组分配为1,否则分配为0。 按月汇总支出总额,它是列日期,但 按月份总计realpurchase_cash
来汇总utc_time.y
所以2个月的984的总和,而realpurchase_cash = 25的总和,所以flag = 0
每个组最多可以保留90天的数据。
I.E.output
我决定使用sqldf解决方案,因为我使用sql 我这样做
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
")
并得到错误
Error in result_create(conn@ptr, statement) : near "over": syntax error
如何纠正?
答案 0 :(得分:2)
我认为问题是为什么会出现错误。
在RSQLite升级到SQLite数据库的最新版本之前,窗口将不起作用。而是使用RPostgreSQL后端。对于该后端,请使用"..."
而不是[...]
,还可以解决问题中显示的sql语句中的拼写和其他错误。
不会产生语法错误(假设已安装并运行PostgreSQL服务器)。
library(sqldf)
library(RPostgreSQL)
a1s <- sqldf('
SELECT
"generated_id",
"campaign_id.x",
"campaign_id.y",
"spent",
"date",
"utc_time.y",
"realpurchase_cash",
--SUM(spent) over (partition by generated_id,[campaign_id.x],[campaign_id.y]) as sum_spent,
--SUM(realpurchase_cash) over (partition by generated_id,[campaign_id.x],[campaign_id.y]) as sum_realpurchase_cash
CASE WHEN SUM("realpurchase_cash") OVER
(PARTITION BY "generated_id", "campaign_id.x", "campaign_id.y") >
SUM(spent) OVER (PARTITION BY "generated_id", "campaign_id.x", "campaign_id.y")
THEN 1 ELSE 0
END AS "flag"
FROM "mydata"')
给予:
> a1s
generated_id campaign_id.x campaign_id.y spent date utc_time.y realpurchase_cash flag
1 1.003477e+15 2.384307e+16 2.384307e+16 73.50 04.10.2018 07.10.2018 15:55 1.49 0
2 1.003477e+15 2.384307e+16 2.384307e+16 73.50 04.10.2018 30.09.2018 12:00 1.49 0
3 1.003477e+15 2.384307e+16 2.384307e+16 73.50 04.10.2018 05.10.2018 22:34 1.49 0
4 1.003477e+15 2.384307e+16 2.384307e+16 73.50 04.10.2018 01.10.2018 22:26 1.49 0
5 1.003477e+15 2.384307e+16 2.384307e+16 73.50 04.10.2018 06.10.2018 13:43 1.49 0
6 1.003477e+15 2.384307e+16 2.384307e+16 73.50 04.10.2018 06.10.2018 13:43 1.49 0
7 1.003477e+15 2.384307e+16 2.384307e+16 73.50 04.10.2018 30.09.2018 12:23 1.49 0
8 1.003477e+15 2.384307e+16 2.384307e+16 73.50 04.10.2018 30.09.2018 18:12 1.49 0
9 1.003477e+15 2.384307e+16 2.384307e+16 73.50 04.10.2018 30.09.2018 11:22 1.49 0
10 1.003477e+15 2.384307e+16 2.384307e+16 73.50 04.10.2018 05.10.2018 22:35 1.49 0
11 1.003477e+15 2.384307e+16 2.384307e+16 73.50 04.10.2018 30.09.2018 11:23 1.49 0
12 1.003477e+15 2.384307e+16 2.384307e+16 29.74 26.09.2018 07.10.2018 15:55 1.49 0
13 1.003477e+15 2.384307e+16 2.384307e+16 29.74 26.09.2018 30.09.2018 12:00 1.49 0
14 1.003477e+15 2.384307e+16 2.384307e+16 29.74 26.09.2018 05.10.2018 22:34 1.49 0
15 1.003477e+15 2.384307e+16 2.384307e+16 29.74 26.09.2018 01.10.2018 22:26 1.49 0
16 1.003477e+15 2.384307e+16 2.384307e+16 29.74 26.09.2018 06.10.2018 13:43 1.49 0
17 1.003477e+15 2.384307e+16 2.384307e+16 29.74 26.09.2018 06.10.2018 13:43 1.49 0