2011-08-29 2 views
15

R은 문자 벡터를 ASCII가 아니라 영문자로 설명하는 시퀀스로 정렬합니다. 예를 들어문자 벡터의 R 정렬 규칙은 무엇입니까?

:

sort(c("dog", "Cat", "Dog", "cat")) 
[1] "cat" "Cat" "dog" "Dog" 

세 가지 질문 :

  1. 이 정렬 순서를 설명하는 기술적으로 올바른 용어는 무엇입니까?
  2. CRAN의 설명서에서 이에 대한 언급을 찾을 수 없습니다. R에서 정렬 규칙에 대한 설명을 어디에서 찾을 수 있습니까?
  3. 이것은 C, Java, Perl 또는 PHP와 같은 다른 언어의 동작과 다릅니다.
+0

관련 항목 [문자열 정렬시 대소 문자를 구분하지 마십시오.] (http://stackoverflow.com/q/4245196/271616). –

답변

21

Details:sort()에 대한 상태 :

The sort order for character vectors will depend on the collating 
sequence of the locale in use: see ‘Comparison’. The sort order 
for factors is the order of their levels (which is particularly 
appropriate for ordered factors). 

help(Comparison) 다음을 보여줍니다

Comparison of strings in character vectors is lexicographicwithin 
the strings using the collating sequence of the locale in use:see 
‘locales’. The collating sequence of locales such as ‘en_US’ is 
normally different from ‘C’ (which should use ASCII) and can be 
surprising. Beware of making _any_ assumptions about the 
collation order: e.g. in Estonian ‘Z’ comes between ‘S’ and ‘T’, 
and collation is not necessarily character-by-character - in 
Danish ‘aa’ sorts as a single letter, after ‘z’. In Welsh ‘ng’ 
may or may not be a single sorting unit: if it is it follows ‘g’. 
Some platforms may not respect the locale and always sort in 
numerical order of the bytes in an 8-bit locale, or in Unicode 
point order for a UTF-8 locale (and may not sort in the same order 
for the same language in different character sets). Collation of 
non-letters (spaces, punctuation signs, hyphens, fractions and so 
on) is even more problematic. 

그래서 당신의 로케일 설정에 따라 달라집니다.

+1

D' oh. 나는 이것을 http://cran.r-project.org/doc/manuals/R-ints.html에서 찾으려고 노력했다. 고맙습니다. – Andrie

+3

더크와 도움의 설명을 개선하려고하지는 않겠지 만 R의 ​​외부에서는 대소 문자가 변하지 않더라도 사전 적 정렬로 설명되어있는 것을 알 수 있습니다. 순열 처리는 일반적으로 영어 순서와 관련하여 이루어 지므로 일부 다른 언어에는 적합하지 않기 때문에 대조 규칙은 심각한 고려 사항입니다. 좋은 예는 엄격한 A-Z 순서로 26 자만을 생각하는 사람이나 원어민에게 이름 정렬을 정말로 이상하게 보입니다. – Iterator

+0

그리고 방금 공간 문자가 무시 될 수도 있고 무시되지 않을 수도 있다는 것을 오랜 시간 동안 알았습니다. 그리고 이것은 테스트를 로컬에서 실행하는지, 아니면'R CMD check'를 수행하는지에 따라 변경되었습니다. –