且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用data.table计数和汇总/汇总列

更新时间:2023-12-01 17:51:22

The post you are referring to gives a method on how to apply one aggregation method to several columns. If you want to apply different aggregation methods to different columns, you can do:

dat[, .(count = .N, var = sum(VAR)), by = MNTH]

这将导致:


     MNTH count var
1: 201501     4   2
2: 201502     3   0
3: 201503     5   2
4: 201504     4   2


您还可以通过以下方式通过更新数据集将这些值添加到现有数据集中:

You can also add these values to your existing dataset by updating your dataset by reference:

dat[, `:=` (count = .N, var = sum(VAR)), by = MNTH]


> dat
      MNTH VAR count var
 1: 201501   1     4   2
 2: 201501   1     4   2
 3: 201501   0     4   2
 4: 201501   0     4   2
 5: 201502   0     3   0
 6: 201502   0     3   0
 7: 201502   0     3   0
 8: 201503   0     5   2
 9: 201503   0     5   2
10: 201503   1     5   2
11: 201503   1     5   2
12: 201503   0     5   2
13: 201504   1     4   2
14: 201504   0     4   2
15: 201504   1     4   2
16: 201504   0     4   2


有关如何使用语法,请参见入门指南 在GitHub Wiki上。

For further reading about how to use data.table syntax, see the Getting started guides on the GitHub wiki.