'R Data.Table Aggregate

library(data.table)
set.seed(01)
DATA = data.table("STUDENT" = c(1:2000),
                  "GROUP" = c(sample(letters[1:3], size=2000, r = T)),
                  "CLASS" = c(sample(1:6, size=2000, r = T)),
                  "SCORE1" = c(sample(50:100,size=2000,r=T)),
                  "SCORE2" = c(sample(40:100, size=2000, r = T)))

Say you have this data and you wish to make SEVERAL extra data.table that are aggregated or collapse...

a) mean SCORE1 and mean SCORE2 by GROUP

b) mean SCORE1 and mean SCORE2 by CLASS

c) mean SCORE1 and mean SCORE2 by GROUP and by CLASS

d) count of GROUP by CLASS

Is there more efficient way to use data.table to create these 4 data.table? Or do you simply recommend making all of them and storing as 4 separate data.tables?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source