Skip to contents

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

Usage

# S3 method for array
reorder(x,dim=1,names=NULL,indices=NULL,FUN=mean,...)
# S3 method for 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.

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

Value

The reordered object x.

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.4909692  1.7019227 -0.1575236  0.423516038 -0.4157476
#> B -0.2781602 -0.2191605 -1.6705938 -0.383907602  0.7191832
#> C  1.9902031  0.9765465  2.1993641  0.316010892 -0.7873279
#> D  0.3773306 -0.1496624 -0.2998385 -1.118061234 -0.2933884
#> E  0.2207406 -1.0414609  1.6164087  0.004506212 -0.9921484
  reorder(M,dim=1,names=c("E","A"))
#>            a          b          c            d          e
#> E  0.2207406 -1.0414609  1.6164087  0.004506212 -0.9921484
#> A -0.4909692  1.7019227 -0.1575236  0.423516038 -0.4157476
#> B -0.2781602 -0.2191605 -1.6705938 -0.383907602  0.7191832
#> C  1.9902031  0.9765465  2.1993641  0.316010892 -0.7873279
#> D  0.3773306 -0.1496624 -0.2998385 -1.118061234 -0.2933884
  reorder(M,dim=2,indices=3:1)
#>            c          b          a            d          e
#> A -0.1575236  1.7019227 -0.4909692  0.423516038 -0.4157476
#> B -1.6705938 -0.2191605 -0.2781602 -0.383907602  0.7191832
#> C  2.1993641  0.9765465  1.9902031  0.316010892 -0.7873279
#> D -0.2998385 -0.1496624  0.3773306 -1.118061234 -0.2933884
#> E  1.6164087 -1.0414609  0.2207406  0.004506212 -0.9921484
  reorder(M,dim=1)
#>            a          b          c            d          e
#> B -0.2781602 -0.2191605 -1.6705938 -0.383907602  0.7191832
#> D  0.3773306 -0.1496624 -0.2998385 -1.118061234 -0.2933884
#> E  0.2207406 -1.0414609  1.6164087  0.004506212 -0.9921484
#> A -0.4909692  1.7019227 -0.1575236  0.423516038 -0.4157476
#> C  1.9902031  0.9765465  2.1993641  0.316010892 -0.7873279
  reorder(M,dim=2)
#>            e            d          b          c          a
#> A -0.4157476  0.423516038  1.7019227 -0.1575236 -0.4909692
#> B  0.7191832 -0.383907602 -0.2191605 -1.6705938 -0.2781602
#> C -0.7873279  0.316010892  0.9765465  2.1993641  1.9902031
#> D -0.2933884 -1.118061234 -0.1496624 -0.2998385  0.3773306
#> E -0.9921484  0.004506212 -1.0414609  1.6164087  0.2207406