[R] sorting character vectors

Liaw, Andy andy_liaw at merck.com
Thu Aug 19 14:18:33 CEST 2004


I do get (R-1.9.1 on WinXPPro):

> x <- c(LETTERS[1:3], paste(" ", LETTERS[1:3], sep=""))
> sort(x)
[1] " A" " B" " C" "A"  "B"  "C" 

The `right' sequence is locale dependent, and I wonder if that's the
discrepancy here.

Andy


> From: andreas.krause at pharma.novartis.com
> 
> The following is not what I expected in sorting characters 
> (single letters 
> and the same letters with preceding spaces).
> Can someone enlighten me as to why the following might be a 
> correct result 
> for sorting?
> 
> ; x <- c(LETTERS[1:3], paste(" ", LETTERS[1:3], sep=""))
> ; x
> [1] "A"  "B"  "C"  " A" " B" " C"
> ; sort(x)
> [1] "A"  " A" "B"  " B" "C"  " C"
> ; sort(x, method="shell")
> [1] "A"  " A" "B"  " B" "C"  " C"
> ; sort(x, method="quick")
> [1] "A"  " A" "B"  " B" "C"  " C"
> 
> I would expect the result to be " A" " B" " C" "A"  "B"  "C" instead, 
> going by ASCII codes (and a quick check with S-Plus 6.2 shows 
> that this is 
> what S-Plus thinks the sorted sequence is).
> 
> Thanks,
> 
>         Andreas Krause
> 
> PS. Version specs:
> 
> ; version
>          _ 
> platform i686-pc-linux-gnu
> arch     i686 
> os       linux-gnu 
> system   i686, linux-gnu 
> status 
> major    1 
> minor    9.1 
> year     2004 
> month    06 
> day      21 
> language R
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list