myGDa <- Grid(nlevels = c("X" = 2, "Y" = 3, "Z" = 4))
myGDb <- aperm(myGDa, perm = c(3, 2, 1))
## evaluation of a function on the permuted array
myGD1 <- Grid(nlevels = c("X" = 2, "Y" = 3, "Z" = 4))
myPerm <- c(3, 2, 1)
myFun <- function(vec){ sin(vec["X"]) + vec["Y"] - vec["Z"]^2 }
## 'f1' contains the value of the function in the order of 'myGD1'
f1 <- apply_Grid(myGD1, fun = myFun)
## 'f2' contains the value of the function in the order of 'myGD2'
myGD2 <- aperm(myGD1, perm = myPerm)
f2 <- apply_Grid(myGD2, fun = myFun)
## note that 'as.matrix' sorts the observations in the index order
XYZ1 <- as.matrix(myGD1)
XYZ2 <- as.matrix(myGD2)
## check
f = apply(XYZ1, 1, myFun)
cbind(XYZ1, f1 = f1, XYZ2, f2 = f2, f = f)
eps <- sqrt(.Machine$double.eps)
all(abs(f1 - f) < eps) ## should be TRUE
all(abs(f2 - f) < eps) ## should be TRUE
Run the code above in your browser using DataLab