aperm
Array Permutations

Description

Permutes the dimensions of an array. Optionally, the shape of the array can be held constant while the dimensions are changed.

Usage

aperm(a, perm, ...)
## Default S3 method:
aperm(a, perm = NULL, resize = TRUE, ...)
## S3 method for class 'table':
aperm(a, perm = NULL, resize = TRUE, keep.class = TRUE, ...)

Arguments

a the array (an object such that attr(a,"dim") is not NULL) to be permuted. Missing values (NAs) are allowed.
perm a vector containing a permutation of the integers 1:n, where n is the number of dimensions in the array a. That is, n == length(dim(a)). The old dimension given by perm[j] becomes the new j-th dimension. perm may also be a permutation of the names(dimnames(a)), as long as those names exist and are distinct from one another. If perm is missing, the dimensions are reversed. (That is, perm = n:1.)
resize a logical flag. If TRUE (the default), the dimensions of the result are changed to correspond with the re-ordering. If FALSE, the dimensions of the result are the same as the dimensions of a and the result will have no dimnames.
keep.class a logical value. If TRUE, keep the class of returned value as the class of a.
... additional arguments that other methods for this generic function might use.
Value
returns an array like a, but with the observations permuted according to perm. For example, if perm is c(2,1,3), the result is an array in which the old second dimension is the new first dimension, and so on.
See Also
t for the transpose of a matrix.
Examples
x <- array(c(1001:1024),
           dim = c(2, 3, 4),
           dimnames = list(Row=c("r1", "r2"),
                           Col=c("c1", "c2", "c3"),
                           Slice=c("s1", "s2", "s3", "s4")))
aperm(x, c(2, 1, 3))  # turns 2 x 3 x 4 into 3 x 2 x 4.
aperm(x, c("Col", "Row", "Slice")) # do the same with names(dimnames(x))
tbl <- with(Sdatasets::fuel.frame,
    table(Heavy=Weight>3000, Type, HiMileage=Mileage>=33))
aperm(tbl, c("HiMileage", "Type", "Heavy"))
Package base version 6.0.0-69
Package Index