Permutation of dimensions, or generalized transpose

Share:

Description

Permutation of dimensions, or generalized transpose.

Arguments

a

Object to be transposed.

perm

Integer vector giving the permutation.

...

Additional argument list.

Details

The generalised transposition here is equivalent to the corresponding permutation of the columns of the data frame or matrix obtained with as.data.frame or as.matrix. The order of the rows of the data frame or matrix is left unchanged, and is determined by the index slot of the object. Thus if a vector or matrix of response, say Y, has its elements or rows aligned with the design this property remains after the transposition, see Examples. It must be kept in mind that this "design" order can not be guessed when only the factors and their levels are known.

Value

An object with the same class as a but with permuted dimensions.

See Also

aperm S3 method from the base package.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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