Reorder an Array or Matrix

Description

reorder.array reorders an array along a specified dimension according given names, indices or results of a function applied.

Usage

## S4 method for signature 'array'
reorder(x,dim=1,names=NULL,indices=NULL,FUN=mean,...)
## S4 method for signature 'matrix'
reorder(x,dim=1,names=NULL,indices=NULL,FUN=mean,...)

Arguments

x

An array

dim

An integer specifying the dimension along which x should be ordered.

names

A character vector

indices

A numeric vector

FUN

A function that can be used in apply(x,dim,FUN)

...

further arguments, ignored.

Value

The reordered object x.

Details

Typical usages are

reorder(x,dim,names)
reorder(x,dim,indices)
reorder(x,dim,FUN)

The result of rename(x,dim,names) is x reordered such that dimnames(x)[[dim]] is equal to the concatenation of those elements of names that are in dimnames(x)[[dim]] and the remaining elements of dimnames(x)[[dim]].

The result of rename(x,dim,indices) is x reordered along dim according to indices.

The result of rename(x,dim,FUN) is x reordered along dim according to order(apply(x,dim,FUN)).

See also

The default method of reorder in package stats.

Examples

(M <- matrix(rnorm(n=25),5,5,dimnames=list(LETTERS[1:5],letters[1:5])))
            a          b           c           d          e
A -0.09884583  0.6279462 -0.78160806 -0.06150576  2.2657186
B -0.07404970 -0.3468971  1.16362714 -0.65859668 -0.3366611
C  0.20808846  1.4896055  0.56263593 -0.25449704  1.0129528
D  0.17213351  1.4365645 -0.07504326 -0.50511874  1.3945779
E  1.26273417 -0.6268383  1.49832919  1.15661203 -0.4996854
reorder(M,dim=1,names=c("E","A"))
            a          b           c           d          e
E  1.26273417 -0.6268383  1.49832919  1.15661203 -0.4996854
A -0.09884583  0.6279462 -0.78160806 -0.06150576  2.2657186
B -0.07404970 -0.3468971  1.16362714 -0.65859668 -0.3366611
C  0.20808846  1.4896055  0.56263593 -0.25449704  1.0129528
D  0.17213351  1.4365645 -0.07504326 -0.50511874  1.3945779
reorder(M,dim=2,indices=3:1)
            c          b           a           d          e
A -0.78160806  0.6279462 -0.09884583 -0.06150576  2.2657186
B  1.16362714 -0.3468971 -0.07404970 -0.65859668 -0.3366611
C  0.56263593  1.4896055  0.20808846 -0.25449704  1.0129528
D -0.07504326  1.4365645  0.17213351 -0.50511874  1.3945779
E  1.49832919 -0.6268383  1.26273417  1.15661203 -0.4996854
reorder(M,dim=1)
            a          b           c           d          e
B -0.07404970 -0.3468971  1.16362714 -0.65859668 -0.3366611
A -0.09884583  0.6279462 -0.78160806 -0.06150576  2.2657186
D  0.17213351  1.4365645 -0.07504326 -0.50511874  1.3945779
E  1.26273417 -0.6268383  1.49832919  1.15661203 -0.4996854
C  0.20808846  1.4896055  0.56263593 -0.25449704  1.0129528
reorder(M,dim=2)
            d           a           c          b          e
A -0.06150576 -0.09884583 -0.78160806  0.6279462  2.2657186
B -0.65859668 -0.07404970  1.16362714 -0.3468971 -0.3366611
C -0.25449704  0.20808846  0.56263593  1.4896055  1.0129528
D -0.50511874  0.17213351 -0.07504326  1.4365645  1.3945779
E  1.15661203  1.26273417  1.49832919 -0.6268383 -0.4996854