Reorder an Array or Matrix
reorder.array.Rd
reorder.array
reorders an array along a specified
dimension according given names, indices or results of
a function applied.
Usage
# S3 method for class 'array'
reorder(x,dim=1,names=NULL,indices=NULL,FUN=mean,...)
# S3 method for class 'matrix'
reorder(x,dim=1,names=NULL,indices=NULL,FUN=mean,...)
Details
Typical usages are
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))
.
Examples
(M <- matrix(rnorm(n=25),5,5,dimnames=list(LETTERS[1:5],letters[1:5])))
#> a b c d e
#> A 1.8743386 -1.306719969 0.10740376 0.2539096 -2.0246515
#> B -0.4549758 -0.617825577 -0.96500473 1.1744065 -1.0179494
#> C 0.3976505 -0.785040344 -1.44962002 -0.6803192 -0.1569594
#> D -0.4710842 -0.001056473 0.30988795 0.2582959 -1.1550237
#> E -0.7351499 -1.416252938 -0.07050849 0.4159256 0.5324802
reorder(M,dim=1,names=c("E","A"))
#> a b c d e
#> E -0.7351499 -1.416252938 -0.07050849 0.4159256 0.5324802
#> A 1.8743386 -1.306719969 0.10740376 0.2539096 -2.0246515
#> B -0.4549758 -0.617825577 -0.96500473 1.1744065 -1.0179494
#> C 0.3976505 -0.785040344 -1.44962002 -0.6803192 -0.1569594
#> D -0.4710842 -0.001056473 0.30988795 0.2582959 -1.1550237
reorder(M,dim=2,indices=3:1)
#> c b a d e
#> A 0.10740376 -1.306719969 1.8743386 0.2539096 -2.0246515
#> B -0.96500473 -0.617825577 -0.4549758 1.1744065 -1.0179494
#> C -1.44962002 -0.785040344 0.3976505 -0.6803192 -0.1569594
#> D 0.30988795 -0.001056473 -0.4710842 0.2582959 -1.1550237
#> E -0.07050849 -1.416252938 -0.7351499 0.4159256 0.5324802
reorder(M,dim=1)
#> a b c d e
#> C 0.3976505 -0.785040344 -1.44962002 -0.6803192 -0.1569594
#> B -0.4549758 -0.617825577 -0.96500473 1.1744065 -1.0179494
#> E -0.7351499 -1.416252938 -0.07050849 0.4159256 0.5324802
#> A 1.8743386 -1.306719969 0.10740376 0.2539096 -2.0246515
#> D -0.4710842 -0.001056473 0.30988795 0.2582959 -1.1550237
reorder(M,dim=2)
#> b e c a d
#> A -1.306719969 -2.0246515 0.10740376 1.8743386 0.2539096
#> B -0.617825577 -1.0179494 -0.96500473 -0.4549758 1.1744065
#> C -0.785040344 -0.1569594 -1.44962002 0.3976505 -0.6803192
#> D -0.001056473 -1.1550237 0.30988795 -0.4710842 0.2582959
#> E -1.416252938 0.5324802 -0.07050849 -0.7351499 0.4159256