Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the default language of date in R

Tags:

r

I used a R package written by other people. In the package, it is supposed to create a file name as "Mar.12". However the file name is "三月.12” in my system since I am running it on an OS with Chinese language (windows 10). I have changed the display language to English in the Rconsole file but it does not help. I am wondering is there any method to change the default date to English in R without revising the original package?

Thanks in advance for help!

like image 748
user2230101 Avatar asked Sep 06 '16 03:09

user2230101


People also ask

What is the default date format in R language?

Dates in R display in a string format and can be output in various ways, the default being YYYY-MM-DD. The most commonly used symbols to format dates are given in the table below.

How do I change the default language in R?

You may select the language of R‑Studio main panel and its help. To do so, select an available language on Change Language on the Help menu. You may set which panels and bars to enable/​disable. Sometimes, there may be a lot of similar objects in the Drives panel.

How do I change the date format in R?

To format = , provide a character string (in quotes) that represents the current date format using the special “strptime” abbreviations below. For example, if your character dates are currently in the format “DD/MM/YYYY”, like “24/04/1968”, then you would use format = "%d/%m/%Y" to convert the values into dates.

What data type should dates be in R?

Unless you need the list nature of the POSIXlt class, the POSIXct class is the usual choice for storing dates in R. If your input date/times are stored as the number of seconds from January 1, 1970, you can create POSIX date values by assigning the appropriate class directly to those values.


2 Answers

More info : https://stat.ethz.ch/R-manual/R-devel/library/base/html/locales.html

You'll get all your local variables through : sessionInfo()

Exemple From R doc :

Sys.getlocale()

Sys.getlocale("LC_TIME")

Set your language

Windows

  • Sys.setlocale("LC_TIME", "German")
  • Sys.setlocale("LC_TIME", "English")
  • Sys.setlocale("LC_TIME", "French")

Other

  • Sys.setlocale("LC_TIME", "de") # Solaris: details are OS-dependent
  • Sys.setlocale("LC_TIME", "de_DE") # Many Unix-alikes
  • Sys.setlocale("LC_TIME", "de_DE.UTF-8") # Linux, macOS, other Unix-alikes
  • Sys.setlocale("LC_TIME", "de_DE.utf8") # some Linux versions
like image 100
Delje Avatar answered Oct 18 '22 19:10

Delje


OK, as a Q&A site, it seems an answer is required. From your description, it appears to be the issue of your locales. Read ?locales for more.

You can have a test with this (read ?strptime for various format, and pay special attention to those sensitive to locales):

format(Sys.Date(), format = "%Y-%b-%d")
# [1] "2016- 9月-06"

The output has the month in Chinese. If I want to change the display, I need to set "LC_TIME" locale to "C":

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

Then it is OK:

format(Sys.Date(), "%Y-%b-%d")
# [1] "2016-Sep-06"

Every time you start a new R session, you get back to native setting. Should you want a permanent change, put

.First <- function() {
   Sys.setlocale("LC_TIME", "C")
   }

in the $(R RHOME)/etc/Rprofile.site file. Read ?Startup for how to customize R startup and the use of .First.

like image 42
Zheyuan Li Avatar answered Oct 18 '22 19:10

Zheyuan Li