Skip to content
Prev 17866 / 63424 Next

vector labels are not permuted properly in a call to sort() (R 2.1)

The main problem is that R is inconsistent here.  There are lots of 
branches through the sort() code.  Greg showed one.  Here are four more
[,1] [,2]
A    1    5
B    2    6
C    3    7
D    4    8
h g f e d c b a
1 2 3 4 5 6 7 8
[,1] [,2]
A    1    5
B    2    6
C    3    7
D    4    8
attr(,"names")
[1] "h" "g" "f" "e" "d" "c" "b" "a"
[,1] [,2]
A    1    5
B    2    6
C    3    7
D    4    8
attr(,"names")
[1] "a" "b" "c" "d" "e" "f" "g" "h"

I believe Svr4 does keep names but does not allow names on matrices.

There are other problems: should sorting a time-series preserve the ts 
properties (probably not, but it does).  Should (S3 or S4) class 
information be preserved (it seems inappropriate for a time series, for 
example)?

The course of least resistance here is to always preserve attributes and 
to document that we do so.  Probably the most S-compliant solution is to 
preserve only names (and sort them as now).

David James quotes the Blue Book, but note that S itself no longer follows 
the principle stated there.
On Wed, 5 Oct 2005, Martin Maechler wrote: