[R] Sorting factors

jim holtman jholtman at gmail.com
Fri Dec 21 17:02:39 CET 2007


By default, factors are characters and sorted in alphabetical order.
It looks like somehow you numeric data was converted to factors.
Therefore '10' comes before '2'.  If you want the factors in numeric
order you have to convert them back.  Look in the FAQs.

> x <- c(1,2,3,4,5,10,11,20,21,22,30)
> sort(x)
 [1]  1  2  3  4  5 10 11 20 21 22 30
> x.f <- factor(as.character(x))
> str(x.f)
 Factor w/ 11 levels "1","10","11",..: 1 4 8 10 11 2 3 5 6 7 ...
> sort(x.f) # alphabetical order
 [1] 1  10 11 2  20 21 22 3  30 4  5
Levels: 1 10 11 2 20 21 22 3 30 4 5
> sort(as.numeric(as.character(x.f))) # 'expected' numeric order
 [1]  1  2  3  4  5 10 11 20 21 22 30
>


On Dec 21, 2007 10:34 AM, tom sgouros <tomfool at as220.org> wrote:
>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list