[R] sorting character vectors

andreas.krause@pharma.novartis.com andreas.krause at pharma.novartis.com
Thu Aug 19 13:47:34 CEST 2004


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




More information about the R-help mailing list