Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R lubridate: weekdays in local language

How can I get the weekdays and month in my local language?

My code:

library(lubridate)
data <- c("10-02-2015", "11-03-2015")
data.lubri <- dmy(data)
wday(data.lubri, label=TRUE)

Always returns

[1] Tues Wed 
Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat

and

month(data.lubri, label = TRUE)

Always returns

[1] Feb Mar
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec

I need it in Portuguese, using lubridate, and already tried a ton of locale options, but nothing lubridate related seems to work.

The base functions, weekdays() and months(), work, though.

The problem is base months() gives me a unordered vector when working with dates.

I need them as an ordered factor, for later plotting.

My current work around is getting my hands dirty:

factor(months(data.lubri, abbreviate=TRUE), 
             levels = c("Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", 
                        "Ago", "Set", "Out", "Nov", "Dez"), 
             ordered=TRUE)


[1] Fev Mar
Levels: Jan < Fev < Mar < Abr < Mai < Jun < Jul < Ago < Set < Out < Nov < Dez

but this isn't classy...

like image 400
erickfis Avatar asked Oct 17 '25 07:10

erickfis


1 Answers

Current versions of lubridate (at least 1.7.1) allow this by default:

wday(x, label = FALSE, abbr = TRUE,
  week_start = getOption("lubridate.week.start", 7),
  locale = Sys.getlocale("LC_TIME"))

In older versions, you can do simply add labels to the factor variable that hold your week days (Dutch in this example):

variable <- factor(variable, levels = c('Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'), labels = c('Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag', 'Zondag'))
like image 70
Patrick Hubers Avatar answered Oct 18 '25 21:10

Patrick Hubers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!