gpt4 book ai didi

通过data.table non-equi join的相对窗口运行总和

转载 作者:行者123 更新时间:2023-12-04 09:15:09 26 4
gpt4 key购买 nike

我有一个数据集 customerId、transactionDate、productId、purchaseQty 加载到 data.table 中。对于每一行,我想计算前 45 天的购买数量和平均值

        productId customerID transactionDate purchaseQty
1: 870826 1186951 2016-03-28 162000
2: 870826 1244216 2016-03-31 5000
3: 870826 1244216 2016-04-08 6500
4: 870826 1308671 2016-03-28 221367
5: 870826 1308671 2016-03-29 83633
6: 870826 1308671 2016-11-29 60500

我正在寻找这样的输出:
    productId customerID transactionDate purchaseQty    sumWindowPurchases
1: 870826 1186951 2016-03-28 162000 162000
2: 870826 1244216 2016-03-31 5000 5000
3: 870826 1244216 2016-04-08 6500 11500
4: 870826 1308671 2016-03-28 221367 221367
5: 870826 1308671 2016-03-29 83633 305000
6: 870826 1308671 2016-11-29 60500 60500

因此,sumWindowPurchases 包含从当前交易日期起 45 天窗口内客户/产品的购买数量总和。一旦我开始工作,抛出平均值和我需要的其他计算应该是微不足道的

我回到了我的 SQL 根源并想到了自连接:
select   DT.customerId, DT.transactionDate, DT.productId, sum(DT1.purchaseQty)
from DT
inner join DT as DT1 on
DT.customerId = DT1.customerId
and DT.productId = DT1.productId
and DT1.transactionDate between DT.transactionDate and dateadd(day, -45, DT.transactionDate)

尝试使用 data.dable 语法将其转换为 R,我希望做这样的事情:
DT1 <- DT #alias. have confirmed this is just a pointer
DT[DT1[DT1$transactionDate >= DT$transactionDate - 45],
.(sum(DT1$purchaseQty)),
by = .(DT$customerId , DT$transactionDate ),
on = .(customerId , DT1$transactionDate <= DT$TransactionDate),
allow.cartesian = TRUE]

我想我有两个部分的问题。什么是“R方式”来做到这一点。 data.table self join 是正确的方法,还是我最好尝试使用 Reduce 函数?

我怀疑自我加入是获得滚动 45 天窗口的唯一方法。所以第 2 部分是我需要一些有关 data.table 语法的帮助,以明确引用列来自哪个源表,因为它是自连接并且它们具有相同的列名。

我一直在研究弗兰克链接到的答案并提出了这个表达
DT[.(p = productId, c = customerID, t = transactionDate, start = transactionDate - 45),
on = .(productId==p, customerID==c, transactionDate<=t, transactionDate>=start),
allow.cartesian = TRUE, nomatch = 0]

产生这个输出:
   productId customerID transactionDate purchaseQty transactionDate.1
1: 870826 1186951 2016-03-28 162000 2016-02-12
2: 870826 1244216 2016-03-31 5000 2016-02-15
3: 870826 1244216 2016-04-08 5000 2016-02-23
4: 870826 1244216 2016-04-08 6500 2016-02-23
5: 870826 1308671 2016-03-28 221367 2016-02-12
6: 870826 1308671 2016-03-29 221367 2016-02-13
7: 870826 1308671 2016-03-29 83633 2016-02-13
8: 870826 1308671 2016-11-29 60500 2016-10-15

这非常接近,我需要达到我的最后一步。如果我可以总结这个输出的购买数量,按客户/产品/交易日期.1 分组,我会有一些有用的东西。但是,我无法理解语法,我不明白 transactionDate.1 名称来自哪里

最佳答案

首先,我们找出当前日期(包括当前日期)之前的 45 天窗口中发生了多少个交易日期

setDT(df)
df[, n:= 1:.N - findInterval(transactionDate - 45, transactionDate), by=.(customerID)]
df
# productId customerID transactionDate purchaseQty n
#1: 870826 1186951 2016-03-28 162000 1
#2: 870826 1244216 2016-03-31 5000 1
#3: 870826 1244216 2016-04-08 6500 2
#4: 870826 1308671 2016-03-28 221367 1
#5: 870826 1308671 2016-03-29 83633 2
#6: 870826 1308671 2016-11-29 60500 1
接下来我们找到 purchaseQty 的滚动和带窗口大小 n .采用一个很好的答案 here
g <- function(x, window){
b_pos <- seq_along(x) - window + 1 # begin positions
cum <- cumsum(x)
cum - cum[b_pos] + x[b_pos]
}
df[, sumWindowPurchases := g(purchaseQty, n),][,n:=NULL,]
df
# productId customerID transactionDate purchaseQty sumWindowPurchases
#1: 870826 1186951 2016-03-28 162000 162000
#2: 870826 1244216 2016-03-31 5000 5000
#3: 870826 1244216 2016-04-08 6500 11500
#4: 870826 1308671 2016-03-28 221367 221367
#5: 870826 1308671 2016-03-29 83633 305000
#6: 870826 1308671 2016-11-29 60500 60500

数据
structure(list(productId = c(870826L, 870826L, 870826L, 870826L, 
870826L, 870826L), customerID = c(1186951L, 1244216L, 1244216L,
1308671L, 1308671L, 1308671L), transactionDate = structure(c(16888,
16891, 16899, 16888, 16889, 17134), class = "Date"), purchaseQty = c(162000L,
5000L, 6500L, 221367L, 83633L, 60500L)), .Names = c("productId",
"customerID", "transactionDate", "purchaseQty"), row.names = c("1:",
"2:", "3:", "4:", "5:", "6:"), class = "data.frame")

关于通过data.table non-equi join的相对窗口运行总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41007099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com