Home > front end >  The wrong language when extracting date from datetime using as.Date function
The wrong language when extracting date from datetime using as.Date function

Time:12-14

I extract date using this piece of code : all_trips$day_of_week <- format(as.Date(all_trips$date), "%A")

I get days of the week in Russian language (I'm using Windows 10 and it's also in Russian),but I'd like to get them in English since everything else in my R environment is set to English.

I tried this : Sys.setlocale("LC_TIME", "English") it didn't help

Session info:
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22518)

Matrix products: default

locale:
[1] LC_COLLATE=Russian_Russia.1251  LC_CTYPE=Russian_Russia.1251   
[3] LC_MONETARY=Russian_Russia.1251 LC_NUMERIC=C                   
[5] LC_TIME=Russian_Russia.1251    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.1.2 tools_4.1.2

CodePudding user response:

I couldn't use Sys.setlocale on windows, too.

Found an alternative that works quite good (just replace Sys.time() with your date column):

c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", 
  "Friday", "Saturday")[as.POSIXlt(Sys.time())$wday   1]

Output:

[1] "Monday"

CodePudding user response:

I found one solution

 Sys.setlocale(category = "LC_ALL", locale = "English")

it does the trick,but it resets back to my system language once I restart R session.

Now I'm wondering how to stop my locale settings from resetting everytime I restart the session.

  • Related