Subset by group with data.table

2018-12-31 03:18发布

Assume I have a data table containing some baseball players:

library(plyr)
library(data.table)

bdt <- as.data.table(baseball)

For each player (given by id), I want to find the row corresponding to the year in which they played the most games. This is straightforward in plyr:

ddply(baseball, "id", subset, g == max(g))

What's the equivalent code for data.table?

I tried:

setkey(bdt, "id") 
bdt[g == max(g)]  # only one row
bdt[g == max(g), by = id]  # Error: 'by' or 'keyby' is supplied but not j
bdt[, .SD[g == max(g)]] # only one row

This works:

bdt[, .SD[g == max(g)], by = id] 

But it's is only 30% faster than plyr, suggesting it's probably not idiomatic.

1条回答
孤独寂梦人
2楼-- · 2018-12-31 03:22

Here's the fast data.table way:

bdt[bdt[, .I[g == max(g)], by = id]$V1]

This avoids constructing .SD, which is the bottleneck in your expressions.

edit: Actually, the main reason the OP is slow is not just that it has .SD in it, but the fact that it uses it in a particular way - by calling [.data.table, which at the moment has a huge overhead, so running it in a loop (when one does a by) accumulates a very large penalty.

查看更多
登录 后发表回答