I have
XIa <- diag(1, 3)
colnames(XIa) <- rownames(XIa) <- c("a0", "a1", "a2")
XIb <- diag(1, 2)
colnames(XIb) <- rownames(XIb) <- c("b0", "b1")
XIc <- diag(1, 2)
colnames(XIc) <- rownames(XIc) <- c("c0", "c1")
tidyr::expand_grid
gives me:
tidyr::expand_grid(as.data.frame(XIa), as.data.frame(XIb), as.data.frame(XIc))
# A tibble: 12 x 7
a0 a1 a2 b0 b1 c0 c1
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 0 0 1 0 1 0
2 1 0 0 1 0 0 1
3 1 0 0 0 1 1 0
4 1 0 0 0 1 0 1
5 0 1 0 1 0 1 0
6 0 1 0 1 0 0 1
7 0 1 0 0 1 1 0
8 0 1 0 0 1 0 1
9 0 0 1 1 0 1 0
10 0 0 1 1 0 0 1
11 0 0 1 0 1 1 0
12 0 0 1 0 1 0 1
How do I achieve the same result using data.table
?
Clearly, there is this way:
dXIa <- data.table(XIa)
dXIb <- data.table(XIb)
dXIc <- data.table(XIc)
cbind(
dXIa[c(rep(1:3, each = 4))],
dXIb[c(rep(1:2, each = 2))],
dXIc[c(rep(1:2, len = 12))]
)
a0 a1 a2 b0 b1 c0 c1
1: 1 0 0 1 0 1 0
2: 1 0 0 1 0 0 1
3: 1 0 0 0 1 1 0
4: 1 0 0 0 1 0 1
5: 0 1 0 1 0 1 0
6: 0 1 0 1 0 0 1
7: 0 1 0 0 1 1 0
8: 0 1 0 0 1 0 1
9: 0 0 1 1 0 1 0
10: 0 0 1 1 0 0 1
11: 0 0 1 0 1 1 0
12: 0 0 1 0 1 0 1
but that is probably not optimal/ideal.