我正在处理时间序列数据,我需要计算匹配条件的当前行之前的行数。例如,我需要知道行的月份之前的几个月和客户的销售额(NETSALES> 0)。理想情况下,我会维护一个行计数器,当条件失败时重置(例如NETSALES = 0)。
解决问题的另一种方法是标记任何超过12个NETSALES之前的行。
我最接近的是使用
COUNT(*)
OVER (PARTITION BY cust ORDER BY dt
ROWS 12 PRECEDING) as CtWindow,
http://sqlfiddle.com/#!6/990eb/2
在上面的示例中,201310被正确标记为12,但理想情况下,前一行应为11。
解决方案可以是R或T-SQL。
更新了data.table示例:
library(data.table)
set.seed(50)
DT <- data.table(NETSALES=ifelse(runif(40)<.15,0,runif(40,1,100)), cust=rep(1:2, each=20), dt=1:20)
目标是计算如下所示的“运行”列 - 当值为零时重置为零
NETSALES cust dt run
1: 36.956464 1 1 1
2: 83.767621 1 2 2
3: 28.585003 1 3 3
4: 10.250524 1 4 4
5: 6.537188 1 5 5
6: 0.000000 1 6 6
7: 95.489944 1 7 7
8: 46.351387 1 8 8
9: 0.000000 1 9 0
10: 0.000000 1 10 0
11: 99.621881 1 11 1
12: 76.755104 1 12 2
13: 64.288721 1 13 3
14: 0.000000 1 14 0
15: 36.504473 1 15 1
16: 43.157142 1 16 2
17: 71.808349 1 17 3
18: 53.039105 1 18 4
19: 0.000000 1 19 0
20: 27.387369 1 20 1
21: 58.308899 2 1 1
22: 65.929296 2 2 2
23: 20.529473 2 3 3
24: 58.970898 2 4 4
25: 13.785201 2 5 5
26: 4.796752 2 6 6
27: 72.758112 2 7 7
28: 7.088647 2 8 8
29: 14.516362 2 9 9
30: 94.470714 2 10 10
31: 51.254178 2 11 11
32: 99.544261 2 12 12
33: 66.475412 2 13 13
34: 8.362936 2 14 14
35: 96.742115 2 15 15
36: 15.677712 2 16 16
37: 0.000000 2 17 0
38: 95.684652 2 18 1
39: 65.639292 2 19 2
40: 95.721081 2 20 3
NETSALES cust dt run
答案 0 :(得分:3)
这似乎是这样做的:
library(data.table)
set.seed(50)
DT <- data.table(NETSALES=ifelse(runif(40)<.15,0,runif(40,1,100)), cust=rep(1:2, each=20), dt=1:20)
DT[,dir:=ifelse(NETSALES>0,1,0)]
dir.rle <- rle(DT$dir)
DT <- transform(DT, indexer = rep(1:length(dir.rle$lengths), dir.rle$lengths))
DT[,runl:=cumsum(dir),by=indexer]
归功于Cumulative sums over run lengths. Can this loop be vectorized?
罗兰编辑:
以下是更好的表现并考虑不同的客户:
#no need for ifelse
DT[,dir:= NETSALES>0]
#use a function to avoid storing the rle, which could be huge
runseq <- function(x) {
x.rle <- rle(x)
rep(1:length(x.rle$lengths), x.rle$lengths)
}
#never use transform with data.table
DT[,indexer := runseq(dir)]
#include cust in by
DT[,runl:=cumsum(dir),by=list(indexer,cust)]
编辑:joe添加了SQL解决方案 http://sqlfiddle.com/#!6/990eb/22
SQL解决方案在一台机器上有48分钟的时间,在22米的行中有128克的内存。在具有4 gig ram的工作站上,R解决方案约为20秒。去R!