编辑:这个问题是由于安装了过时的 data.table 版本。
我有一个如下的 data.table:
require(xts)
a <- data.table(colour=c("Red","Green","Blue","Blue","Black","Black"), date=c(as.Date("2011-07-04"),as.Date("2011-07-10"),as.Date("2011-07-09"),as.Date("2011-07-12"),as.Date("2011-07-04"),as.Date("2011-07-09")),daily.quantity=c(1,-1,2,-2,1,1))
colour date daily.quantity
[1,] Red 2011-07-04 1
[2,] Green 2011-07-10 -1
[3,] Blue 2011-07-09 2
[4,] Blue 2011-07-12 -2
[5,] Black 2011-07-04 1
[6,] Black 2011-07-09 1
我希望每种颜色的累积总数看起来像这样:
colour date daily.quantity cumulative.quantity
[1,] Black 2011-07-04 1 1
[2,] Black 2011-07-09 1 2
[3,] Blue 2011-07-09 2 2
[4,] Blue 2011-07-12 -2 0
[5,] Green 2011-07-10 -1 -1
[6,] Red 2011-07-04 1 1
但是,如果我尝试以下操作,我最终会得到不考虑颜色的累积总数:
setkey(a,colour,date)
a[,cumulative.quantity := cumsum(daily.quantity)]
colour date daily.quantity cumulative.quantity
[1,] Black 2011-07-04 1 1
[2,] Black 2011-07-09 1 2
[3,] Blue 2011-07-09 2 4
[4,] Blue 2011-07-12 -2 2
[5,] Green 2011-07-10 -1 1
[6,] Red 2011-07-04 1 2
我尝试了明显的,但不幸的是未实现:
> a[,cumulative.quantity := cumsum(daily.quantity),keyby="colour,date"]
Error in `[.data.table`(a, , `:=`(cumulative.quantity, cumsum(daily.quantity)), :
Combining := in j with by is not yet implemented. Please let maintainer('data.table') know if you are interested in this.
那么,任何人都可以为此提出解决方法吗?