R data.table计数面板数据

时间:2013-09-05 20:13:43

标签: r count data.table

我有面板数据(科目/年),我希望只保留每年出现最多次数的科目。数据集很大所以我使用的是data.table包。有没有比我在下面尝试过的更优雅的解决方案?

library(data.table)

DT <- data.table(SUBJECT=c(rep('John',3), rep('Paul',2), 
                           rep('George',3), rep('Ringo',2), 
                           rep('John',2), rep('Paul',4), 
                           rep('George',2), rep('Ringo',4)), 
                 YEAR=c(rep(2011,10), rep(2012,12)), 
                 HEIGHT=rnorm(22), 
                 WEIGHT=rnorm(22))
DT

DT[, COUNT := .N, by='SUBJECT,YEAR']
DT[, MAXCOUNT := max(COUNT), by='YEAR']

DT <- DT[COUNT==MAXCOUNT]
DT <- DT[, c('COUNT','MAXCOUNT') := NULL]
DT

1 个答案:

答案 0 :(得分:14)

我不确定你会将其视为优雅,但如何:

DT[, COUNT := .N, by='SUBJECT,YEAR']
DT[, .SD[COUNT == max(COUNT)], by='YEAR']

本质上是如何将by应用于i表达式,如@SenorO所评论的那样。之后你仍然需要[,COUNT:=NULL]但是需要一个临时列而不是两个。

出于速度原因,我们不鼓励.SD,但希望我们很快就会收到此功能请求,以便删除建议:FR#2330 Optimize .SD[i] query to keep the elegance but make it faster unchanged.

不同的方法如下。它更快更惯用,但可能被认为不那么优雅。

# Create a small aggregate table first. No need to use := on the big table.
i = DT[, .N, by='SUBJECT,YEAR']

# Find the even smaller subset. (Do as much as we can on the small aggregate.)
i = i[, .SD[N==max(N)], by=YEAR]

# Finally join the small subset of key values to the big table
setkey(DT, YEAR, SUBJECT)
DT[i]

类似的东西是here