[R] sorting character vectors

Roger D. Peng rpeng at jhsph.edu
Thu Aug 19 14:16:23 CEST 2004


Sorting depends on the locale.  For example, I get

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

On Linux (Fedora Core 1) I set LANG=C, which is necessary for some 
other (non-R) things to work.

-roger

andreas.krause at pharma.novartis.com wrote:
> 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