I want to do the same as explained here, i.e. adding missing rows to a data.table. The only additional difficulty I\'m facing is that I want the number of key columns, i.e.
Michael's answer is great. do.call
is indeed needed to call CJ
flexibly in that way, afaik.
To clear up on the expression building approach and starting with your code, but removing the df$
parts (not needed and not done in the linked answer, since i
is evaluated within the scope of DT
) :
str <- ""
for (i in idCols) {
str <- paste0(str, "unique(", i, "), ")
}
str <- paste0(str, "seq(from=min(variable), to=max(variable))")
str
[1] "unique(fundID), unique(cfType), seq(from=min(variable), to=max(variable))"
then it's :
expr <- parse(text=paste0("CJ(",str,")"))
DT[eval(expr),nomatch=NA]
or alternatively build and eval the whole query dynamically :
eval(parse(text=paste0("DT[CJ(",str,"),nomatch=NA")))
And if this is done a lot then it may be worth creating yourself a helper function :
E = function(...) eval(parse(text=paste0(...)))
to reduce it to :
E("DT[CJ(",str,"),nomatch=NA")