模仿data.table中的tapply行为

时间:2017-07-06 09:13:06

标签: r data.table grouping

我希望tapply使用data.table来模仿保留因素,例如

library(data.table)
amounts <- data.table(year = factor(2014:2017, levels = 2010:2017),
                      amount = 1:4)
tapply(amounts$amount, amounts$year, sum)

产量

2010 2011 2012 2013 2014 2015 2016 2017 
  NA   NA   NA   NA    1    2    3    4 

但是,使用data.table的方法不会显示没有行的年份:

amounts[ , list(sumAmount = sum(amount)), by = year]

   year sumAmount
1: 2014         1
2: 2015         2
3: 2016         3
4: 2017         4

data.table中是否有一种干净的方法?

2 个答案:

答案 0 :(得分:4)

包含缺失级别的惯用方法是使用连接,我认为:

amounts[.(levels(year)), on=.(year), list(sumAmount = sum(amount)), by = .EACHI]

   year sumAmount
1: 2010        NA
2: 2011        NA
3: 2012        NA
4: 2013        NA
5: 2014         1
6: 2015         2
7: 2016         3
8: 2017         4

答案 1 :(得分:2)

一种选择是使用dcast

中的data.table
dcast(amounts, 1 ~ year, value.var = 'amount', sum, drop = FALSE, fill = NA)