Q:

Aggregate sum of means where grouping is not nested

Consider the following Table:
set.seed(4) 
DT=data.table(GroupeID=rep(LETTERS[1:5],each=4)
          ,ID=letters[rep(sample(1:26,5),times=2,each=2)]
          ,numVar=runif(20)
          );DT

   GroupeID ID     numVar
 1:        A  p 0.26042777
 2:        A  p 0.72440589
 3:        A  a 0.90609215
 4:        A  a 0.94904022
 5:        B  h 0.07314447
 6:        B  h 0.75467503
 7:        B  g 0.28600062
 8:        B  g 0.10005352
 9:        C  r 0.95406878
10:        C  r 0.41560712
11:        C  p 0.45510242
12:        C  p 0.97105566
13:        D  a 0.58398798
14:        D  a 0.96220462
15:        D  h 0.76170240
16:        D  h 0.71450854
17:        E  g 0.99661286
18:        E  g 0.50627089
19:        E  r 0.48994324
20:        E  r 0.64916137

I am trying to add a column which shows the sum of the means of numVar for each ID by GroupeID.
This is of course not the same as simply adding up numVar by GroupeID, since an ID can occur in multiple GroupeID. The desired column sumPG can be created in two steps:
DT[, meanID:=mean(numVar), by=ID]
DT[, sumPG:=sum(meanID), by=GroupeID]

Is this possible in one step in data.table? I've tried this:
DT[, sumPG:=sum(DT[,.(GroupeID, mean(numVar)),by=ID]$V2), by=GroupeID];DT

but this does not do the grouping. Can I somehow subset the inner DT to only include GroupeID from the outer group after grouping by ID?

A:

DT[, meanID:=mean(numVar), by=ID][, sumPG:=sum(meanID), by=GroupeID][,-c("meanID")]

