I had to create indexes for subsets and then use data.table dt[,foo(.SD),by=subset_ID]
, but I'm not sure how to do this, since I take a selection with a replacement (several group identifiers).
When merging, you can have overlapping groups:
# convert to numeric
dt[, b := as.numeric(b)]
# make samples
set.seed(1)
mDT = setDT(melt(replicate(1000, sample(letters,5))))
setnames(mDT, c("seqi", "g", "a"))
# compute function on each sample
dt[mDT, on=.(a), allow.cartesian=TRUE, .(g, b)][, .(res = mean(b)), by=g]
which gives
g res
1: 1 50017.85
2: 2 49980.03
3: 3 50093.80
4: 4 50087.67
5: 5 49990.83
---
996: 996 50013.11
997: 997 50095.43
998: 998 49913.61
999: 999 50058.44
1000: 1000 49909.36
To confirm this, you can check, for example,
dt[a %in% mDT[g == 1, a], mean(b)]
# [1] 50017.85
, ( ), RAM-.
mean
, ; . ?GForce
, b
.
( , , ), , .
, , :
dtagg = dt[, .(.N, sumb = sum(b)), by=a]
dtagg[mDT, on=.(a), .(g, sumb, N)][, lapply(.SD, sum), by=g][, .(g, res = sumb/N)]
g res
1: 1 50017.85
2: 2 49980.03
3: 3 50093.80
4: 4 50087.67
5: 5 49990.83
---
996: 996 50013.11
997: 997 50095.43
998: 998 49913.61
999: 999 50058.44
1000: 1000 49909.36
allow.cartesian
, mDT
dtagg
. , , :