Skip to content

sort() leaves row names unaffected

3 messages · Martin Wegmann, Uwe Ligges, PIKAL Petr

#
Hello, 

I wonder if I ran into a bug. If I do 

summary(df1$X1) -> df1.y

df1.y
a  b   c   d  e
[1,] 50.74627 8.955224 17.91045 19.40299 2.985075

sort(df1.y) 
       a  b   c   d  e
[1,] 2.985075 8.955224 17.91045 19.40299 50.74627

my numbers are sorted but do not anymore correspond to the rownames. 
For me it is counterintuitive that solely the numbers are sorted and not the 
names. Is there a way to sort names + numbers or is this behaviour of sort() 
unintended?

Martin

R 2.0.1-1 debian reposit.
#
Martin Wegmann wrote:

            
Note: You are sorting a matrix rather than a vector!
You mean colnames, not rownames!
Don't use sort() on matrices, use order() instead, as in:

  x[,order(x)]

Uwe Ligges
#
Hi

I am not sure how you did get such result of sort and summary

Summary usually gives
Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.000   1.050   1.250   1.143   1.369   2.000 

and sorting it
Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  5.000   1.050  10.000   1.143   1.369   2.000
1st Qu.    Mean 3rd Qu.    Max.    Min.  Median 
  1.050   1.143   1.369   2.000   5.000  10.000 

as well as
a          b          c          d          e 
-3.8849080  1.9393499  1.8456569 -2.5334288  0.6304933
a          d          e          c          b 
-3.8849080 -2.5334288  0.6304933  1.8456569  1.9393499
On 14 Dec 2004 at 12:23, Martin Wegmann wrote:
Didn't you redefine something?

Cheers
Petr
Petr Pikal
petr.pikal at precheza.cz