French names using wday in lubridate
Asked Answered
C

2

6

The wday() function in the lubridate package with the option label = TRUE returns the name of the day of the week in English. I'd like to know if it is possible to get the name of day of the week in another language. Is there any option for that ?

Cantharides answered 24/7, 2013 at 14:29 Comment(1)
You could use base weekdays.Jabez
J
4

Not without writing your own method.

The days of the week are hardcoded in English in lubridate:::wday.numeric

labels <- c("Sunday", "Monday", "Tuesday", "Wednesday", 
            "Thursday", "Friday", "Saturday")

You could tweak the code from my answer here and replace the English names with names in the language of your choice.

# assuming x is your Date
c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", 
   "Friday", "Saturday")[as.POSIXlt(x)$wday + 1]

Edit:

Here is a version that more closely matches lubridate

labels <- c("dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi")
ordered(as.POSIXlt(x)$wday + 1, levels=1:7, labels=labels)
Josi answered 24/7, 2013 at 14:33 Comment(2)
I've just discovered the weekdays() function which return results in the local language.Cantharides
Update 2021: after setting my system environment to French, Sys.setlocale("LC_TIME", "French"), lubridate's wday returns french labels.Oe
O
3

New answer to an old question:

Either tell your system to use French throughout:

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

Or set it within the function itself:

day <- seq(ymd("2018-01-01"), ymd("2018-01-08"), "day")
wday(day, label = TRUE, abbr = TRUE, locale = "EN-us")

[1] Mon Tue Wed Thu Fri Sat Sun Mon
Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat

wday(day, label = TRUE, abbr = TRUE, locale = "French")

[1] lun\\. mar\\. mer\\. jeu\\. ven\\. sam\\. dim\\. lun\\.
Levels: dim\\. < lun\\. < mar\\. < mer\\. < jeu\\. < ven\\. < sam\\.

I've got to figure out why the labels are showing up with slashes though. I've asked the package maintainers about this because the month labels aren't showing up properly either (for me).

https://github.com/tidyverse/lubridate/issues/1127

One hint that helped me get proper months (e.g., février not f<e9>vrier) was to change the encoding, e.g., use locale = "fr_CA.utf8"... but this didn't fix the weekday labels.

Oe answered 26/7, 2023 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.