You can change the key for that purpose.
setkey(d1,s,u)
After that command all u
values for the same s
value are grouped together.
u s
1: 20 1
2: 35 1
3: 36 1
4: 87 1
5: 123 1
---
996: 208 100
997: 262 100
998: 352 100
999: 430 100
1000: 455 100
Operations performed on the groups defined by the key columns usually work really fast, e.g.
d1[,mean(u),keyby='s']
If you need to do fast aggregation for both groups u
and s
, you could store two instances of the data.table
. For one you use setkey(d1,u,s)
and for the other setkey(d1,s,u)
. If you want to perform operations quickly on the groups defined by the values of u
use the former data.table otherwise the latter.