reorder.array memisc 0.99.20.1

# 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))`.

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.7341410 -0.7576405  2.2486328  0.8347612 -1.01033778
B -0.8300679 -0.9868882 -0.8295705  1.4926334  0.04336509
C  0.9205102  0.9412825  2.4673668  0.3822467 -0.97064760
D  0.5141975  0.1228477 -1.2796099 -1.1406843 -1.01391489
E -1.7228716 -0.4889968 -0.9487092  1.1529502 -1.24634339
```
```reorder(M,dim=1,names=c("E","A"))
```
```           a          b          c          d           e
E -1.7228716 -0.4889968 -0.9487092  1.1529502 -1.24634339
A  0.7341410 -0.7576405  2.2486328  0.8347612 -1.01033778
B -0.8300679 -0.9868882 -0.8295705  1.4926334  0.04336509
C  0.9205102  0.9412825  2.4673668  0.3822467 -0.97064760
D  0.5141975  0.1228477 -1.2796099 -1.1406843 -1.01391489
```
```reorder(M,dim=2,indices=3:1)
```
```           c          b          a          d           e
A  2.2486328 -0.7576405  0.7341410  0.8347612 -1.01033778
B -0.8295705 -0.9868882 -0.8300679  1.4926334  0.04336509
C  2.4673668  0.9412825  0.9205102  0.3822467 -0.97064760
D -1.2796099  0.1228477  0.5141975 -1.1406843 -1.01391489
E -0.9487092 -0.4889968 -1.7228716  1.1529502 -1.24634339
```
```reorder(M,dim=1)
```
```           a          b          c          d           e
E -1.7228716 -0.4889968 -0.9487092  1.1529502 -1.24634339
D  0.5141975  0.1228477 -1.2796099 -1.1406843 -1.01391489
B -0.8300679 -0.9868882 -0.8295705  1.4926334  0.04336509
A  0.7341410 -0.7576405  2.2486328  0.8347612 -1.01033778
C  0.9205102  0.9412825  2.4673668  0.3822467 -0.97064760
```
```reorder(M,dim=2)
```
```            e          b          a          c          d
A -1.01033778 -0.7576405  0.7341410  2.2486328  0.8347612
B  0.04336509 -0.9868882 -0.8300679 -0.8295705  1.4926334
C -0.97064760  0.9412825  0.9205102  2.4673668  0.3822467
D -1.01391489  0.1228477  0.5141975 -1.2796099 -1.1406843
E -1.24634339 -0.4889968 -1.7228716 -0.9487092  1.1529502
```