R lubridate: weekdays in local language

How can I get 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 this in portuguese using lubridate and have tried a ton of locale options already but nothing seems like lubridate.

The basic functions, weekdays () and months (), work, however.

Problem - base months () gives me an unordered vector when dealing with dates.

I need them as an ordered multiplier for subsequent construction.

My current job is fooling my hands:

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 it's not cool ...

+3


source to share


2 answers


@erickfis,

I've created a function that summarizes your approach to any language, as I've encountered the same problem often:



months2<-function(date){
x<-format(ISOdate(1970, 1:12, 1), "%B")
factor(months(date),ordered=TRUE,levels=x)
}

      

+1


source


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 simply add labels to the variable of factors that hold your weekly 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'))

      

+1


source







All Articles