Get weekdays in English in R
Asked Answered
G

7

24

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?

Goatish answered 10/6, 2013 at 19:7 Comment(6)
That is caused by your locale setting in Linux/UNIX Try: (Sys.setenv("LANGUAGE"="En"). I assume the same works in Windows, but I do not know first hand.Putup
Thank you jim mcnamara, I am using Ubuntu but even after the Sys.setenv I still get the weeekdays() in Spanish!Goatish
I do not have an answer then. Sorry.Putup
Does this work: Sys.setlocale("LC_TIME", "en_US"); weekdays(Sys.Date()+0:6)?Andresandresen
Thank you Josh O'Brien, this is what I get: Warning message: In Sys.setlocale("LC_TIME", "en_US") : OS reports request to set locale to "en_US" cannot be honoredGoatish
Cool. I'll add the *NIX version to my answer.Andresandresen
O
27

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.

Orthopterous answered 10/6, 2013 at 19:21 Comment(5)
as a restricted user, this doesn't work for me on either windows or linux - "OS reports request ... cannot be honored"Seay
It may be a different argument to Sys.setlocale. On a Mac the back-to-English argument is "en_US.UTF-8".Nonpayment
Thank you all! I believe this works in Ubuntu: > Sys.setlocale("LC_TIME", "C")Goatish
@Seay -- 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"))?Andresandresen
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.Seay
S
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.

Safekeeping answered 10/9, 2014 at 17:9 Comment(0)
H
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]
Hagridden answered 10/6, 2013 at 20:11 Comment(3)
are you sure this isn't locale dependent? the weekday doesn't start on Sunday in all countriesSeay
@eddi, I'm pretty sure because POSIXlt is based on struct tm and tm_wday is "days since Sunday" ... edit: and because ?POSIXlt says wday is "0-6 day of the week, starting on Sunday."Hagridden
list_wday_en <- c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat") wday_now <- list_wday_en[wday(now())]Sonnysonobuoy
L
9

Under windows RStudio

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

That was the only thing that worked for me.

Lamaism answered 6/8, 2014 at 21:47 Comment(0)
O
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!

Oscular answered 4/6, 2015 at 9:38 Comment(0)
S
1

How about this:

dev_null <- Sys.setlocale("LC_TIME", "english")
Sisal answered 1/3, 2020 at 19:42 Comment(2)
How does that differ from the answer by @EdgarGithub?Barneybarnhart
It will not cause this warning: Warning message: In Sys.setlocale("LC_TIME", "english") : OS reports request to set locale to "en_US" cannot be honored @MrBeanBremenSisal
R
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)
Recency answered 28/5, 2022 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.