有没有更优雅的方法来使用 lubridate 将两位数的年份转换为四位数的年份? [英] Is there a more elegant way to convert two-digit years to four-digit years with lubridate?

查看:23
本文介绍了有没有更优雅的方法来使用 lubridate 将两位数的年份转换为四位数的年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果日期向量有两位数的年份,mdy() 将 00 到 68 之间的年份转换为 21 世纪年份,将 69 到 99 之间的年份转换为 20 世纪年份.例如:

If a date vector has two-digit years, mdy() turns years between 00 and 68 into 21st Century years and years between 69 and 99 into 20th Century years. For example:

library(lubridate)    
mdy(c("1/2/54","1/2/68","1/2/69","1/2/99","1/2/04"))

给出以下输出:

Multiple format matches with 5 successes: %m/%d/%y, %m/%d/%Y.
Using date format %m/%d/%y.
[1] "2054-01-02 UTC" "2068-01-02 UTC" "1969-01-02 UTC" "1999-01-02 UTC" "2004-01-02 UTC"

我可以通过从错误的日期中减去 100 来解决这个问题,将 2054 和 2068 变成 1954 和 1968 年.但是有没有更优雅、更不容易出错的解析两位数日期的方法,以便它们得到处理在解析过程中是否正确?

I can fix this after the fact by subtracting 100 from the incorrect dates to turn 2054 and 2068 into 1954 and 1968. But is there a more elegant and less error-prone method of parsing two-digit dates so that they get handled correctly in the parsing process itself?

更新:@JoshuaUlrich 将我指向 strptime 后,我发现了 这个问题,它处理的问题与我的类似,但使用的是基础 R.

Update: After @JoshuaUlrich pointed me to strptime I found this question, which deals with an issue similar to mine, but using base R.

R 中日期处理的一个很好的补充似乎是在日期解析函数中处理两位数日期的世纪选择截止的某种方式.

It seems like a nice addition to date handling in R would be some way to handle century selection cutoffs for two-digit dates within the date parsing functions.

推荐答案

这是一个允许您执行此操作的函数:

Here is a function that allows you to do this:

library(lubridate)
x <- mdy(c("1/2/54","1/2/68","1/2/69","1/2/99","1/2/04"))


foo <- function(x, year=1968){
  m <- year(x) %% 100
  year(x) <- ifelse(m > year %% 100, 1900+m, 2000+m)
  x
}

试试看:

x
[1] "2054-01-02 UTC" "2068-01-02 UTC" "1969-01-02 UTC" "1999-01-02 UTC"
[5] "2004-01-02 UTC"

foo(x)
[1] "2054-01-02 UTC" "2068-01-02 UTC" "1969-01-02 UTC" "1999-01-02 UTC"
[5] "2004-01-02 UTC"

foo(x, 1950)
[1] "1954-01-02 UTC" "1968-01-02 UTC" "1969-01-02 UTC" "1999-01-02 UTC"
[5] "2004-01-02 UTC"

<小时>

这里的神奇之处在于使用模运算符 %% 返回除法的小数部分.所以 1968 %% 100 产生 68.


The bit of magic here is to use the modulus operator %% to return the fraction part of a division. So 1968 %% 100 yields 68.

这篇关于有没有更优雅的方法来使用 lubridate 将两位数的年份转换为四位数的年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆