2

编辑:这个问题是由于安装了过时的 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.

那么,任何人都可以为此提出解决方法吗?

4

2 回答 2

3

您不希望按“日期”和“颜色”进行总计,只需要按“颜色”。不确定为什么需要 xts,因为 data.table 在 pkg:data.table 中。

> a[,cumulative.quantity := cumsum(daily.quantity), by=c("colour") ]
   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

如果您实际上确实 wnat 了两列(这意味着您的“想要看起来像这样”的示例是错误的,您可以这样做:

> setkey(a,colour,date)
> a[,cumulative.quantity := cumsum(daily.quantity), by=c("colour", "date") ]
   colour       date daily.quantity cumulative.quantity
1:  Black 2011-07-04              1                   1
2:  Black 2011-07-09              1                   1
3:   Blue 2011-07-09              2                   2
4:   Blue 2011-07-12             -2                  -2
5:  Green 2011-07-10             -1                  -1
6:    Red 2011-07-04              1                   1
于 2012-08-13T18:32:28.993 回答
2

分组by应该只在colour

a[,cumulative.quantity := cumsum(daily.quantity), by=colour]
   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
于 2012-08-13T18:32:35.653 回答