A bit awkward but this should do it:
setkey(x, id1)
(setkey(x[id1.dt], id2))[
id2.dt, allow.cartesian=T][
order(val), -(1:2)
]
Produces:
val fid fid.1
1: 0.2 1232 334
2: 0.2 3224 334
3: 0.2 3434 334
4: 0.2 1232 443
5: 0.2 3224 443
6: 0.2 3434 443
7: 0.3 234 344
8: 0.3 231 344
9: 0.3 234 24
10: 0.3 231 24
11: 0.5 332 334
12: 0.5 332 443
You can also try merge.data.table
to achieve a similar result in a somewhat more intuitively graspable form:
merge(
merge(x, id1.dt, by="id1"),
id2.dt, by="id2", allow.cartesian=T
)[, -(1:2)]