在此MVCE代码段中,为什么不能以这种方式使用.SD[1:round]
?
有没有办法将data.table
转变为提交?
> test <- data.table(event = c(1,1,1,2,2), round = c(seq(1,3),seq(1,2)), cost = seq(20,60,10))
> test
event round cost
1: 1 1 20
2: 1 2 30
3: 1 3 40
4: 2 4 50
5: 2 5 60
> test[,.(cumulative=sum(.SD[1:round]$cost)),by=.(event)]
event cumulative
1: 1 20
2: 2 NA
Warning messages:
1: In 1:round : numerical expression has 3 elements: only the first used
2: In 1:round : numerical expression has 2 elements: only the first used
期望的结果:
event round cumulative
1: 1 1 20
2: 1 2 50
3: 1 3 90
4: 2 1 50
5: 2 2 110