For multiple dimensions, you need some more tildes (if I understood your question correctly):
acast(y, Var1 ~ Var2 ~ Var3)
# , , 1
#
# 1 2
# 1 1 3
# 2 2 4
#
# , , 2
#
# 1 2
# 1 1 3
# 2 2 4
To generalize this behavior, you can try constructing a formula
with paste
:
Form <- as.formula(paste(grep("^Var", names(y), value=TRUE),
collapse = " ~ "))
acast(y, Form)