I'm looking to normalize a variable in a data.table by subtracting the mean within each group. I have done it in the following manner:
dx <- data.table(x=c(1,3,5,1,8,11),group=factor(c(1,1,1,2,2,2)))
dy <- dx[,.(xmean=mean(x)),by=.(group)]
setkey(dx,group)
setkey(dy,group)
dx[dy,x_norm:=x-xmean]
I'm wondering if there is a more concise way of doing this?