聚合和DCast

时间:2014-11-25 16:08:55

标签: r data.table

我有一个data.table,内容如下:

      betnr   persnr status year
1: 50536224 60961963 normal 1997
2: 50536224 62841998 normal 1997
3: 50536224 62841998 normal 1998
4: 50536224 60961963   fire 1998
5: 50536224 62841998   fire 1999
6: 50536224 60961963   hire 1996
7: 50536224 62841998   hire 1996
8: 50536224 63000343  SHORT 1998

我想汇总betnryear发生的不同status次数。以下是我的方法:

aggregateNStatus <- test[, .N, by=list(betnr, year, status)]
dcast.data.table(aggregateNStatus, betnr + year  ~ status)
      betnr year SHORT fire hire normal
1: 50536224 1996    NA   NA    2     NA
2: 50536224 1997    NA   NA   NA      2
3: 50536224 1998     1    1   NA      1
4: 50536224 1999    NA    1   NA     NA

这正是我想要的,但看起来很麻烦。鉴于data.table非常棒,有没有办法一步到位?

1 个答案:

答案 0 :(得分:3)

您不需要aggregateNStatus

 dcast.data.table(test, betnr+year~status, length,
                       fill=NA_integer_, value.var='status')
 #     betnr year SHORT fire hire normal
#1: 50536224 1996    NA   NA    2     NA
#2: 50536224 1997    NA   NA   NA      2
#3: 50536224 1998     1    1   NA      1
#4: 50536224 1999    NA    1   NA     NA