非英语缩写月份名称不一致(丹麦语和挪威语)

时间:2018-06-15 11:28:40

标签: r readr locals

我希望将挪威语中的某些日期转换为R中的实际日期。我使用readr,它有点有效 - 但我偶然发现了一个让我烦恼的问题,我不会&#39 ;我真的知道如何绕过它。 以下是我的问题的说明:

> parse_date(c("29. mai 2017", "29. sep 2017"), format = "%d. %b %Y", locale = locale("nn")) 
Warning: 1 parsing failure.
row # A tibble: 1 x 4 col     row   col expected            actual       expected   <int> <int> <chr>               <chr>        actual 1     2    NA date like %d. %b %Y 29. sep 2017
[1] "2017-05-29" NA          

所以它在5月捕获日期,但不是9月份的日期。事实证明,这是因为挪威语9月的缩写需要&#34;。&#34; (sep.代替sep),而May缩写则没有(可能因为它实际上不是缩写; - )):

locale("nb")
<locale>
Numbers:  123,456.78
Formats:  %AD / %AT
Timezone: UTC
Encoding: UTF-8
<date_names>
Days:   søndag (søn.), mandag (man.), tirsdag (tir.), onsdag (ons.), torsdag(tor.), fredag (fre.), lørdag (lør.)
Months: januar (jan.), februar (feb.), mars (mar.), april (apr.), mai (mai), juni (jun.), juli (jul.), august (aug.), september (sep.), oktober (okt.), november (nov.), desember (des.)
AM/PM:  a.m./p.m.

然而,似乎不一致的是,所有月份都不需要相同数量的承租人。我也注意到这些令人讨厌的&#34;。&#34;不是英文缩写的一部分:

> locale("en")
<locale>
Numbers:  123,456.78
Formats:  %AD / %AT
Timezone: UTC
Encoding: UTF-8
<date_names>
Days:   Sunday (Sun), Monday (Mon), Tuesday (Tue), Wednesday (Wed), Thursday (Thu), Friday (Fri), Saturday (Sat)
Months: January (Jan), February (Feb), March (Mar), April (Apr), May (May), June (Jun), July (Jul), August (Aug),
    September (Sep), October (Oct), November (Nov), December (Dec)
AM/PM:  AM/PM

这真的是非常不方便,因为我认为实际上包括&#34;有些罕见。&#34;当注册日期与缩写(但实际上只是基于个人喜好和经验)。任何输入都非常感谢。

2 个答案:

答案 0 :(得分:1)

您可以像这样手动编辑区域设置...

loc <- locale("nb")

loc$date_names$mon_ab <- substr(loc$date_names$mon_ab, 1, 3) #just take first 3 characters

parse_date(c("29. mai 2017", "29. sep 2017"), format = "%d. %b %Y", locale = loc)

[1] "2017-05-29" "2017-09-29"

答案 1 :(得分:0)

与@Andrew Gustar类似且受到启发的解决方案是创建自己的date_names对象:

loc <- locale("nb")
myNo <- date_names(mon = loc$date_names$mon,
                   mon_ab = substr(loc$date_names$mon_ab, 1, 3),
                   day = loc$date_names$day,
                   day_ab = substr(loc$date_names$day, 1, 3))


parse_date(c("29. mai 2017", "29. sep 2017"), format = "%d. %b %Y", locale = locale(date_names = myNo))

[1] "2017-05-29" "2017-09-29"