reorder.array memisc 0.99.22

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.74662576  0.23232078  1.5257964  0.3989610  0.08037460
B -2.83135398 -0.06817863 -1.3009902 -0.6046695 -0.77096972
C -1.11016287  0.77804439  0.4475487 -1.4837771  0.08582782
D  2.22140947  0.70045825  1.2556960 -2.1215455  0.67383476
E  0.05406889 -0.29486154  0.7317554 -0.9710985 -0.39037924
reorder(M,dim=1,names=c("E","A"))
            a           b          c          d           e
E  0.05406889 -0.29486154  0.7317554 -0.9710985 -0.39037924
A -0.74662576  0.23232078  1.5257964  0.3989610  0.08037460
B -2.83135398 -0.06817863 -1.3009902 -0.6046695 -0.77096972
C -1.11016287  0.77804439  0.4475487 -1.4837771  0.08582782
D  2.22140947  0.70045825  1.2556960 -2.1215455  0.67383476
reorder(M,dim=2,indices=3:1)
           c           b           a          d           e
A  1.5257964  0.23232078 -0.74662576  0.3989610  0.08037460
B -1.3009902 -0.06817863 -2.83135398 -0.6046695 -0.77096972
C  0.4475487  0.77804439 -1.11016287 -1.4837771  0.08582782
D  1.2556960  0.70045825  2.22140947 -2.1215455  0.67383476
E  0.7317554 -0.29486154  0.05406889 -0.9710985 -0.39037924
reorder(M,dim=1)
            a           b          c          d           e
B -2.83135398 -0.06817863 -1.3009902 -0.6046695 -0.77096972
C -1.11016287  0.77804439  0.4475487 -1.4837771  0.08582782
E  0.05406889 -0.29486154  0.7317554 -0.9710985 -0.39037924
A -0.74662576  0.23232078  1.5257964  0.3989610  0.08037460
D  2.22140947  0.70045825  1.2556960 -2.1215455  0.67383476
reorder(M,dim=2)
           d           a           e           b          c
A  0.3989610 -0.74662576  0.08037460  0.23232078  1.5257964
B -0.6046695 -2.83135398 -0.77096972 -0.06817863 -1.3009902
C -1.4837771 -1.11016287  0.08582782  0.77804439  0.4475487
D -2.1215455  2.22140947  0.67383476  0.70045825  1.2556960
E -0.9710985  0.05406889 -0.39037924 -0.29486154  0.7317554