1

I'm trying to sort a vector containing all the different states of America. However, when using the order function, R fails to order those states that start with V or W in the correct order, instead sorting them by the following letter. Like this:

first.states <- c("AL", "AR", "CA", "CO", "AZ", "AK", "CT")
first.states[order(first.states)]

[1] "AK" "AL" "AR" "AZ" "CA" "CO" "CT" #Very good!

last.states <- c("WA", "VA", "WY", "WI", "VT", "WV", "VI")
last.states[order(last.states)]

[1] "VA" "WA" "VI" "WI" "VT" "WV" "WY" #Sad face

Does anybody have a clue about what's happening? arrange from dplyr gives me the correct solution, but I cannot use it for this problem.

Sys.getlocale()
[1] "LC_COLLATE=Swedish_Sweden.1252;LC_CTYPE=Swedish_Sweden.1252;LC_MONETARY=Swedish_Sweden.1252;LC_NUMERIC=C;LC_TIME=Swedish_Sweden.1252"
divibisan
  • 11,659
  • 11
  • 40
  • 58
Johan Larsson
  • 3,496
  • 18
  • 34
  • 8
    Looks like a locale issue, for example in Finnish V and W are equal when ordering. But can't say much more since I don't have information about how R handles locales. – Sami Kuhmonen Aug 28 '15 at 15:20
  • Your code does not produce your output to me because of `locale` settings. What `Sys.getlocale()` returns? – SabDeM Aug 28 '15 at 15:24
  • This is why the posting guide for R-Help suggests you post `sessionInfo()` with every question.... – Spacedman Aug 28 '15 at 15:25
  • 1
    Could've figured. Yeah, I'm in Sweden. `> Sys.getlocale() [1] "LC_COLLATE=Swedish_Sweden.1252;LC_CTYPE=Swedish_Sweden.1252;LC_MONETARY=Swedish_Sweden.1252;LC_NUMERIC=C;LC_TIME=Swedish_Sweden.1252"` How can I get around this? – Johan Larsson Aug 28 '15 at 15:25
  • perhaps [this](http://stackoverflow.com/questions/16347731/how-to-change-the-locale-of-r-in-rstudio) – MichaelChirico Aug 28 '15 at 15:37
  • #MichaelChirico Thanks, I noticed that topic, but I refrained because the help file of Sys.setlocale says "Attempts to change the character set (by Sys.setlocale("LC_TYPE", ), if that implies a different character set) during a session may not work and are likely to lead to some confusion." – Johan Larsson Aug 28 '15 at 15:42

0 Answers0