您需要更改 R 正在运行的语言环境。为您的整个 Windows 安装(这似乎不是最佳的)或在 R 会话中通过以下方式执行此操作:
Sys.setlocale("LC_COLLATE", "C")
您可以使用任何其他有效的语言环境字符串代替"C"
那里,但这应该会让您回到letters
您想要的排序顺序。
阅读?locales
更多。
我想值得一提的是姊妹函数Sys.getlocale()
,它查询语言环境参数的当前设置。因此你可以做
(locCol <- Sys.getlocale("LC_COLLATE"))
Sys.setlocale("LC_COLLATE", "lt_LT")
sort(letters)
Sys.setlocale("LC_COLLATE", locCol)
sort(letters)
Sys.getlocale("LC_COLLATE")
## giving:
> (locCol <- Sys.getlocale("LC_COLLATE"))
[1] "en_GB.UTF-8"
> Sys.setlocale("LC_COLLATE", "lt_LT")
[1] "lt_LT"
> sort(letters)
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "y" "j" "k" "l" "m" "n"
[16] "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "z"
> Sys.setlocale("LC_COLLATE", locCol)
[1] "en_GB.UTF-8"
> sort(letters)
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o"
[16] "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
> Sys.getlocale("LC_COLLATE")
[1] "en_GB.UTF-8"
这当然是@Hadley's Answer在安装devtoolswith_collate()
后显示的更简洁的做法。