How to set the default language of date in R

前端 未结 2 987
無奈伤痛
無奈伤痛 2020-12-03 09:25

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 run

相关标签:
2条回答
  • 2020-12-03 09:54

    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
    0 讨论(0)
  • 2020-12-03 09:57

    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.

    0 讨论(0)
提交回复
热议问题