我希望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
中是否有一种干净的方法?
答案 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)