将dd/mm/yy和dd/mm/yyyy转换为日期 [英] Convert dd/mm/yy and dd/mm/yyyy to Dates

查看:157
本文介绍了将dd/mm/yy和dd/mm/yyyy转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符向量,带有像这样的各种格式的日期

I have some a character vector with dates in various formats like this

dates <- c("23/11/12", "20/10/2012", "22/10/2012" ,"23/11/12")

我想将它们转换为日期.我已经从lubridate包中尝试了很好的dmy,但这是行不通的:

I want to convert these to Dates. I have tried the very good dmy from the lubridate package, but this does not work:

    dmy(dates)
[1] "0012-11-23 UTC" "2012-10-20 UTC" "2012-10-22 UTC" "0012-11-23 UTC"

它将/12年视为0012.

It is treating the /12 year as if it is 0012.

因此,我现在尝试使用正则表达式选择每种类型,并使用as.Date()分别转换为日期.但是,我尝试选择dd/mm/yy的正则表达式仅不起作用.

So I now am trying regular expression to select each type and individually convert to dates using as.Date(). However the regular expression I have tried to select the dd/mm/yy only does not work.

dates[grep('[0-9]{2}/[0-9]{2}/[0-9]{2,2}', dates)]

返回

[1] "23/11/12"   "20/10/2012" "22/10/2012" "23/11/12"

我认为{2,2}应该得到正好2个数字,而不是全部.我不太擅长使用正则表达式,因此我们将不胜感激.

I thought that the {2,2} should get a exactly 2 numbers and not all of them. I'm not very good at regular expression so any help will be appreciated.

谢谢

编辑

我实际上拥有的是以下三种不同类型的日期

What I actually have are three different types of date as below

dates <- c("23-Jul-2013", "23/11/12", "20/10/2012", "22/10/2012" ,"23/11/12")

我想将它们转换为日期

parse_date_time(dates,c('dmy'))

给我

[1] "2013-07-23" "0012-11-23" "2012-10-20" "2012-10-22" "0012-11-23"

但是,这是错误的,0012应该是2012.我想(一个非常简单的)解决方案.

However, this is wrong and 0012 should be 2012. I would like (a fairly simple) solution to this.

我现在有一个解决方案(感谢@plannapus)是使用正则表达式 我实际上最终创建了此函数,因为我仍然遇到一些情况,即润滑方法将12变成0012

One solution I now have (thanks to @plannapus)is to use regular expressions I actually ended up creating this function as I was still getting some cases where the lubridate approach was turning 12 into 0012

    asDateRegex <- function(dates, 
        #selects strings from the vector dates using regexes and converts these to Dates
        regexes = c('[0-9]{2}/[0-9]{2}/[0-9]{4}', #dd/mm/yyyy
            '[0-9]{2}/[0-9]{2}/[0-9]{2}$', #dd/mm/yy
            '[0-9]{2}-[[:alpha:]]{3}-[0-9]{4}'), #dd-mon-yyyy
        orders = 'dmy',
        ...){
        require(lubridate)
        new_dates <- as.Date(rep(NA, length(dates)))
        for(reg in regexes){
            new_dates[grep(reg, dates)] <- as.Date(parse_date_time(dates[grep(reg, dates)], order = orders))
        }
        new_dates
    }

asDateRegex (dates)
[1] "2012-10-20" "2013-07-23" "2012-11-23" "2012-10-22" "2012-11-23"

但这不是很优雅.有更好的解决方案吗?

But this is not very elegant. Any better solutions?

推荐答案

您可以在lubridate中使用parse_date_time:

some.dates <- c("23/11/12", "20/10/2012", "22/10/2012" ,"23/11/12")
parse_date_time(some.dates,c('dmy'))
[1] "2012-11-23 UTC" "2012-10-20 UTC" "2012-10-22 UTC" "2012-11-23 UTC"

但是,请注意格式的顺序很重要:

But , Note that the order of format is important :

some.dates <- c("20/10/2012","23/11/12",  "22/10/2012" ,"23/11/12")
parse_date_time(some.dates,c('dmY','dmy'))

[1] "2012-10-20 UTC" "2012-11-23 UTC" "2012-10-22 UTC" "2012-11-23 UTC"

编辑

内部parse_date_time使用的是guess_formats(我猜它使用了一些正则表达式):

Internally parse_date_time is using guess_formats (which I guess uses some regular expressions):

guess_formats(some.dates,c('dmy'))
       dmy        dmy        dmy        dmy 
"%d/%m/%Y" "%d/%m/%y" "%d/%m/%Y" "%d/%m/%y" 

如评论中所述,您可以像这样使用parse_date_time:

As mentioned in the comment you can use parse_date_time like this:

as.Date(dates, format = guess_formats(dates,c('dmy')))

这篇关于将dd/mm/yy和dd/mm/yyyy转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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