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 -1.7146269 1.1727450 1.04450046 -0.1031594 0.8581266
B -0.3030554 -0.3725875 -0.35585174 -0.7766569 -1.2861900
C -1.1881863 0.6824237 0.94627840 -0.2766628 -1.5228105
D 1.3274704 -0.2269835 0.01447379 1.6615574 2.2162591
E -0.5311666 1.1048286 0.08924819 0.7899960 -0.6614911
reorder(M,dim=1,names=c("E","A"))
a b c d e
E -0.5311666 1.1048286 0.08924819 0.7899960 -0.6614911
A -1.7146269 1.1727450 1.04450046 -0.1031594 0.8581266
B -0.3030554 -0.3725875 -0.35585174 -0.7766569 -1.2861900
C -1.1881863 0.6824237 0.94627840 -0.2766628 -1.5228105
D 1.3274704 -0.2269835 0.01447379 1.6615574 2.2162591
reorder(M,dim=2,indices=3:1)
c b a d e
A 1.04450046 1.1727450 -1.7146269 -0.1031594 0.8581266
B -0.35585174 -0.3725875 -0.3030554 -0.7766569 -1.2861900
C 0.94627840 0.6824237 -1.1881863 -0.2766628 -1.5228105
D 0.01447379 -0.2269835 1.3274704 1.6615574 2.2162591
E 0.08924819 1.1048286 -0.5311666 0.7899960 -0.6614911
reorder(M,dim=1)
a b c d e
B -0.3030554 -0.3725875 -0.35585174 -0.7766569 -1.2861900
C -1.1881863 0.6824237 0.94627840 -0.2766628 -1.5228105
E -0.5311666 1.1048286 0.08924819 0.7899960 -0.6614911
A -1.7146269 1.1727450 1.04450046 -0.1031594 0.8581266
D 1.3274704 -0.2269835 0.01447379 1.6615574 2.2162591
reorder(M,dim=2)
a e d c b
A -1.7146269 0.8581266 -0.1031594 1.04450046 1.1727450
B -0.3030554 -1.2861900 -0.7766569 -0.35585174 -0.3725875
C -1.1881863 -1.5228105 -0.2766628 0.94627840 0.6824237
D 1.3274704 2.2162591 1.6615574 0.01447379 -0.2269835
E -0.5311666 -0.6614911 0.7899960 0.08924819 1.1048286