I have a data table:
library(data.table)
(f <- data.table(id1=c(1,2,3,1,2,3),
id2=as.factor(c(\"a\",\"a\",\"b\",\"c\",\"b\",\"d\")),
I'd get the unique values in id1 and id2 and do a join using data.table's cross join function CJ as follows:
# if you've already set the key:
ans <- f[CJ(id1, id2, unique=TRUE)][is.na(v), v := 0L][]
# or, if f is not keyed:
ans <- f[CJ(id1 = id1, id2 = id2, unique=TRUE), on=.(id1, id2)][is.na(v), v := 0L][]
ans