guess_formats + R + lubridate [英] guess_formats + R + lubridate

查看:70
本文介绍了guess_formats + R + lubridate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解如何在lubridate中使用guess_formats函数时遇到麻烦.我有一些未知的日期/格式顺序的日期向量.我想将它们转换为Date对象(或至少转换尽可能多的对象).以下代码是我尝试过的:

I'm having trouble understanding how to use the guess_formats function in lubridate. I have a vector of dates in some unknown set/order of formats. I'd like to convert them to a Date object (or at least convert as many as possible). The following code is what I've tried:

library(lubridate)
sampleDates <- c("4/6/2004","4/6/2004","4/6/2004","4/7/2004",
        "4/6/2004","4/7/2004","2014-06-28","2014-06-30","2014-07-12",
        "2014-07-29","2014-07-29","2014-08-12")
formats <- guess_formats(sampleDates, c("Ymd", "mdY"))
dates <- as.Date(sampleDates, format=formats)

这给出了所有NA.

这显然只是一个简短的例子.在实际情况下,我不知道各种格式的分散位置,也不会100%确定只有%m/%d/%Y和%Y-%m-%d.有人可以让我知道A.在此示例中如何使用guess_formats或B.在lubridate/base R中是否有更适合使用的东西,希望没有大量的正则表达式.谢谢!

This is obviously just a short example. In the real case, I wouldn't know where the various formats are scattered about, and I wouldn't be 100% sure there are only %m/%d/%Y and %Y-%m-%d. Could someone let me know either A. how would guess_formats be used in this example or B. is there something more appropriate to use in lubridate/base R, hopefully without a lot of regex'ing. Thanks!

我也尝试过parse_date_time.我不了解此示例的以下工作:

I've also tried parse_date_time. What I don't understand is the following works for this example:

parse_date_time(sampleDates,
            orders = c("Ymd", "mdY"),
            locale = "eng")

但这不是:

parse_date_time(sampleDates,
            orders = c("mdY", "Ydm"),
            locale = "eng")

在我的实际数据集中,我不知道格式化的顺序,这对于该功能而言似乎很重要.

In my actual set of data, I will not know the order of the formatting, which seems to be important for this function.

双重Dur,好的,我看到在第一个parse_date_time示例中有Ymd,在第二个parse_date_time示例中有Ydm....

Double Dur, OK, I see I had Ymd in the first parse_date_time example and Ydm in the second...carry on.

推荐答案

无需调用guess_formats,只需使用parse_date_time即可:

No need to call guess_formats just use parse_date_time :

 parse_date_time(sampleDates, c("Ymd", "mdY"))

 [1] "2004-04-06 UTC" "2004-04-06 UTC" "2004-04-06 UTC" "2004-04-07 UTC" "2004-04-06 UTC"
 [6] "2004-04-07 UTC" "2014-06-28 UTC" "2014-06-30 UTC" "2014-07-12 UTC" "2014-07-29 UTC"
[11] "2014-07-29 UTC" "2014-08-12 UTC"

内部会调用guess_formats.

这篇关于guess_formats + R + lubridate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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