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.
我需要用葡萄牙语,使用lubridate,并已经尝试了大量的locale选项,但是与lubridate相关的任何东西似乎都不起作用。
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...
但这不是优雅的……
2 个解决方案
#1
1
@erickfis,
@erickfis,
I created a function that generalizes your approach to any locale, as I have faced the same problem frequently:
我创建了一个函数,将您的方法推广到任何语言环境,因为我经常遇到相同的问题:
months2<-function(date){
x<-format(ISOdate(1970, 1:12, 1), "%B")
factor(months(date),ordered=TRUE,levels=x)
}
#2
1
Current versions of lubridate (at least 1.7.1) allow this by default:
润滑油的当前版本(至少1.7.1)默认允许:
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):
在旧版本中,您可以简单地向保存您的工作日的因子变量添加标签(本例中为Dutch):
variable <- factor(variable, levels = c('Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'), labels = c('Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag', 'Zondag'))
#1
1
@erickfis,
@erickfis,
I created a function that generalizes your approach to any locale, as I have faced the same problem frequently:
我创建了一个函数,将您的方法推广到任何语言环境,因为我经常遇到相同的问题:
months2<-function(date){
x<-format(ISOdate(1970, 1:12, 1), "%B")
factor(months(date),ordered=TRUE,levels=x)
}
#2
1
Current versions of lubridate (at least 1.7.1) allow this by default:
润滑油的当前版本(至少1.7.1)默认允许:
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):
在旧版本中,您可以简单地向保存您的工作日的因子变量添加标签(本例中为Dutch):
variable <- factor(variable, levels = c('Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'), labels = c('Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag', 'Zondag'))