[R] Sorting factors

tom sgouros tomfool at as220.org
Fri Dec 21 16:34:23 CET 2007


Hello all:

I'm sorry to take up bandwidth with easy questions, but as an R
beginner, I continue to be surprised by factors.  If someone can shed
some light for me, I'd be grateful:

> d
 Total z02801 z02802 z02804 z02806 z02807 z02808 z02809 z02812 z02813 
 54813     29     51    169   2368    103     76   1328    112    501 
507 Levels: 0 10 1001 1004 1008 1016 1027 1028 103 1031 10318 1043 1045 ... Na
> sort(d)
z02807 z02812 z02809 z02804 z02806 z02801 z02813 z02802  Total z02808 
   103    112   1328    169   2368     29    501     51  54813     76 
507 Levels: 0 10 1001 1004 1008 1016 1027 1028 103 1031 10318 1043 1045 ... Na

Apparently this factor is sorted in alphabetic order, not numeric order.
I find no parameter of sort() that controls for this.

And yet: 

> mode(d)
[1] "numeric"
> length(d)
[1] 10

I would have thought that because d is a numeric list of ten values, I'd
get them sorted in numeric order.

Can someone help me understand why this is expected behavior, and also
what I should do in order to see the numerically sorted list of values I
really want?

Many thanks,

 -tom


-- 
 ------------------------
 tomfool at as220 dot org
 http://sgouros.com  
 http://whatcheer.net



More information about the R-help mailing list