Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
821 views
in Technique[技术] by (71.8m points)

date - Get weekdays in English in R

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...