Вот опция, которая использует data.table
:
library(data.table)
setDT(df)
all_comb <- df[, CJ(Gr2, ID, unique = TRUE), by = Gr1]
df_out <- df[all_comb, on = .(Gr1, Gr2, ID)]
df_out[is.na(df_out)] <- 0
df_out
# Gr1 Gr2 ID var1 var2
# 1: x x1 xa -0.56 -1.07
# 2: x x1 xb -0.23 -0.22
# 3: x x1 xc 1.56 -1.03
# 4: x x1 xd 0.07 -0.73
# 5: x x1 xe 0.13 -0.63
# 6: x x2 xa 0.00 0.00
# 7: x x2 xb 0.00 0.00
# 8: x x2 xc 0.00 0.00
# 9: x x2 xd -0.69 -1.14
# 10: x x2 xe -0.45 1.25
# 11: y y1 ya 0.00 0.00
# 12: y y1 yb 0.36 -0.30
# 13: y y1 yc 0.40 0.90
# 14: y y1 yd 0.11 0.88
# 15: y y1 ye 0.00 0.00
# 16: y y2 ya 1.79 0.69
# 17: y y2 yb 0.50 0.55
# 18: y y2 yc -1.97 -0.06
# 19: y y2 yd 0.70 -0.31
# 20: y y2 ye -0.47 -0.38
PS.
Для пользователей, у которых еще не обновлено до R 3.6, вот данныепроизводится с помощью кода OP в текущей версии R:
df <- structure(list(Gr1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("x", "y"), class = "factor"),
Gr2 = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 4L), .Label = c("x1", "x2", "y1", "y2"), class = "factor"),
ID = structure(c(1L, 2L, 3L, 4L, 5L, 4L, 5L, 7L, 8L, 9L,
6L, 7L, 8L, 9L, 10L), .Label = c("xa", "xb", "xc", "xd",
"xe", "ya", "yb", "yc", "yd", "ye"), class = "factor"), var1 = c(-0.56,
-0.23, 1.56, 0.07, 0.13, -0.69, -0.45, 0.36, 0.4, 0.11, 1.79,
0.5, -1.97, 0.7, -0.47), var2 = c(-1.07, -0.22, -1.03, -0.73,
-0.63, -1.14, 1.25, -0.3, 0.9, 0.88, 0.69, 0.55, -0.06, -0.31,
-0.38)), row.names = c(1L, 2L, 3L, 4L, 5L, 9L, 10L, 12L,
13L, 14L, 16L, 17L, 18L, 19L, 20L), class = "data.frame")