Here is a data.table:
dat = data.table(var1=rnorm(120), var2=rep(c('a','b','c'),40), var3=rep(c(1,2,3,2,1,2,1,2,2,3,1,2),10))
dat2 = dat[,list(resp = mean(var1)),by=list(var2, var3)]
In dat2, only existing interactions of dat$var2 et dat$var3 are present. How can I force dat2 to contain results for all 9 possible interactions (instead of the 7 rows of dat2) for var2 and var3? If there is no direct solutions with data.table, what is the easiest way to solve this issue?
table(dat$var2, dat$var3)
1 2 3
a 20 10 10
b 20 20 0
c 0 30 10
Of course, for the interactions where no data exist in dat, dat2 should contain NA in resp.
You could set the
keyand then do a crossjoin usingCJin theilike so...And by way of explanation,
CJ()creates adata.tablein theiofx(in this casedat) to join on. It is formed as the cross product of the vectors supplied toCJ(), which happens to be precisely what you are looking for!