23

I am using R outside the US and I got everything working in English, but the result of weekdays() is still in Spanish:

Day <- seq(as.Date("2013-06-01"), by=1, len=30)
weekdays(Day)
[1] "sábado"    "domingo"   "lunes"     "martes"    "miércoles"  (...)

Any ideas on how to get the weekdays in English?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Renne007
  • 1,087
  • 2
  • 10
  • 13

7 Answers7

26

Printing of Date and POSIX*t objects seems to be controlled by the LC_TIME locale category.

On Windows, you change it like this:

## First, save the current value so we can restore it later
Sys.getlocale("LC_TIME")
# [1] "English_United States.1252"

## First in Spanish
Sys.setlocale("LC_TIME","Spanish Modern Sort")
# [1] "Spanish_Spain.1252"
weekdays(Sys.Date()+0:6)
# [1] "lunes"     "martes"    "miércoles" "jueves"    "viernes"   "sábado"   
# [7] "domingo"  

## Then back to (US) English
Sys.setlocale("LC_TIME","English United States")
# [1] "English_United States.1252"
weekdays(Sys.Date()+0:6)
# [1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Saturday" 
# [7] "Sunday" 

On most *NIXes, the equivalent would be:

Sys.setlocale("LC_TIME", "en_US")

The particular locale names are OS-dependent, as mentioned in ?Sys.setlocale. For names accepted by Windows, see here. For names accepted by Linux, see here.

r2evans
  • 141,215
  • 6
  • 77
  • 149
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • as a restricted user, this doesn't work for me on either windows or linux - "OS reports request ... cannot be honored" – eddi Jun 10 '13 at 19:25
  • It may be a different argument to `Sys.setlocale`. On a Mac the back-to-English argument is "en_US.UTF-8". – IRTFM Jun 10 '13 at 19:27
  • Thank you all! I believe this works in Ubuntu: > Sys.setlocale("LC_TIME", "C") – Renne007 Jun 10 '13 at 19:31
  • 1
    @eddi -- That's just the error message I get when I use an invalid name for the locale... Out of curiosity, what do you get when you type `Sys.getlocale("LC_TIME")`? Also, does this work, or does it give you an error: `Sys.setlocale("LC_TIME", Sys.getlocale("LC_TIME"))`? – Josh O'Brien Jun 10 '13 at 19:38
  • I see, thanks. I don't get an error with that last expression, and my locale on linux is "C" and is "English_United States.1252" on windows. On windows "Spanish Modern Sort" or "Spanish_Modern_Sort" don't work, but e.g. "Spanish_Argentina" works. I guess it has to do with peculiarities of the OS setup. – eddi Jun 10 '13 at 19:43
13
Sys.setlocale("LC_TIME", "C")

did the trick for me. Also this don't bring us OS reports request to set locale to "EN" cannot be honored error message.

dainys
  • 187
  • 1
  • 8
12

From my answer here, you can get weekdays in English without messing with locales like this:

c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", 
  "Friday", "Saturday")[as.POSIXlt(Day)$wday + 1]
Community
  • 1
  • 1
GSee
  • 48,880
  • 13
  • 125
  • 145
  • are you sure this isn't locale dependent? the weekday doesn't start on Sunday in all countries – eddi Jun 10 '13 at 23:30
  • 3
    @eddi, I'm pretty sure because `POSIXlt` is based on [struct tm](http://www.cplusplus.com/reference/ctime/tm/) and `tm_wday` is "days since Sunday" ... edit: and because `?POSIXlt` says `wday` is "0-6 day of the week, starting on Sunday." – GSee Jun 10 '13 at 23:33
  • list_wday_en <- c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat") wday_now <- list_wday_en[wday(now())] – Andrii Apr 18 '20 at 17:10
9

Under windows RStudio

Sys.setlocale("LC_TIME", "English")

That was the only thing that worked for me.

EdgarGithub
  • 91
  • 1
  • 1
2

I faced the very same problem trying to change the locale from es_ES to en_US (both UTF-8).

R message is given by R main workspace, as it cannot change system locale. If code is inserted into an R-script a new workspace (the running one) is created, and locale can be overriden.

In my code I included the following lines:

curr_locale <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME","en_US.UTF-8")

#<specific code for graph generation>

Sys.setlocale("LC_TIME",curr_locale)

That made the change!

1

How about this:

dev_null <- Sys.setlocale("LC_TIME", "english")
Donatoris
  • 21
  • 4
1

I personally prefer not to modify Sys settings. An alternative solution using the clock package would be:

Date <- seq(as.Date("2013-06-01"), by = 1, len = 30)

# string representation
as.character(clock::date_weekday_factor(Date))

# factor representation
factor(clock::date_weekday_factor(Date, encoding = "iso"), ordered = F)
M W
  • 31
  • 4