I want to calculate the group means of a variable, but excluding the focal responder:
set.seed(1)
dat <- data.table(id = 1:30, y = runif(30), grp = rep(1:3, each=10))
The first record (responder) should have an average value ... second ... and so on:
mean(dat[c==1, y][-1])
mean(dat[c==1, y][-2])
mean(dat[c==1, y][-3])
For the second group the same:
mean(dat[c==2, y][-1])
mean(dat[c==2, y][-2])
mean(dat[c==2, y][-3])
I tried this, but this did not work:
ex[, avg := mean(ex[, y][-.I]), by=grp]
Any ideas?
sdaza source
share