在data.table中使用:= with j(未实现功能的解决方法)

时间:2012-08-13 18:06:47

标签: r data.table

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

那么,有人可以为此建议解决方法吗?

2 个答案:

答案 0 :(得分:3)

您不希望总日期为&#39; date&#39;和&#39;颜色&#39;,只有&#39;颜色&#39;。不确定为什么需要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

如果你确实按两列进行了操作(这意味着你的&#34;想要看起来像这个&#34;例子是错误的,你可以这样做:

> 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

答案 1 :(得分: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