日期转换而不指定格式 [英] Date conversion without specifying the format

查看:429
本文介绍了日期转换而不指定格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白图书馆lubridate中的ymd功能如何在R中工作。我正在尝试构建一个功能,可以正确转换日期,而无需指定格式。我正在检查由于dmy(),mdy()和ymd()函数导致的NA的最小数量。

I do not understand how the "ymd" function from the library "lubridate" works in R. I am trying to build a feature which converts the date correctly without having to specify the format. I am checking for the minimum number of NA's occurring as a result of dmy(), mdy() and ymd() functions.

所以ymd()有时会给出NA,有时不是相同的Date值。在R中是否有其他功能或包,这将有助于我解决这个问题。

So ymd() is giving NA sometimes and sometimes not for the same Date value. Are there any other functions or packages in R, which will help me get over this problem.

> data$DTTM[1:5]
[1] "4-Sep-06"  "27-Oct-06" "8-Jan-07"  "28-Jan-07" "5-Jan-07" 

> ymd(data$DTTM[1])
[1] NA
Warning message:
All formats failed to parse. No formats found. 
> ymd(data$DTTM[2])
[1] "2027-10-06 UTC"
> ymd(data$DTTM[3])
[1] NA
Warning message:
All formats failed to parse. No formats found. 
> ymd(data$DTTM[4])
[1] "2028-01-07 UTC"
> ymd(data$DTTM[5])
[1] NA
Warning message:
All formats failed to parse. No formats found. 
> 

> ymd(data$DTTM[1:5])
[1] "2004-09-06 UTC" "2027-10-06 UTC" "2008-01-07 UTC" "2028-01-07 UTC"
[5] "2005-01-07 UTC"

谢谢

推荐答案

@ user1317221_G已经指出你的日期是日期 - 年份格式,这表明你应该使用 dmy 而不是 ymd 。此外,因为您的月份在%b 格式(当前语言环境中缩写的月份名称);请参阅?strptime ),您的问题可能与您的 locale 有关。您的月份名称似乎是英文,可能与您当前正在使用的语言区域的拼写有所不同。

@user1317221_G has already pointed out that you dates are in day-month-year format, which suggests that you should use dmy instead of ymd. Furthermore, because your month is in %b format ("Abbreviated month name in the current locale"; see ?strptime), your problem may have something to do with your locale. The month names you have seem to be English, which may differ from how they are spelled in the locale you are currently using.

让我们看看当我尝试 dmy 在我的 locale

Let's see what happens when I try dmy on the dates in my locale:

date_english <- c("4-Sep-06",  "27-Oct-06", "8-Jan-07",  "28-Jan-07", "5-Jan-07")
dmy(date_english)

# [1] "2006-09-04 UTC" NA               "2007-01-08 UTC" "2007-01-28 UTC" "2007-01-05 UTC"
# Warning message:
#  1 failed to parse.

-Oct-06无法解析。让我们检查一下我的时间 locale

"27-Oct-06" failed to parse. Let's check my time locale:

Sys.getlocale("LC_TIME")
# [1] "Norwegian (Bokmål)_Norway.1252"

在我的区域设置中将oct识别为有效的%b 月。

dmy does not recognize "oct" as a valid %b month in my locale.

处理此问题的一种方法将oct改为相应的挪威语缩写okt:

One way to deal with this issue would be to change "oct" to the corresponding Norwegian abbreviation, "okt":

date_nor <- c("4-Sep-06",  "27-Okt-06", "8-Jan-07",  "28-Jan-07", "5-Jan-07" )
dmy(date_nor)
# [1] "2006-09-04 UTC" "2006-10-27 UTC" "2007-01-08 UTC" "2007-01-28 UTC" "2007-01-05 UTC"

另一种可能性是使用原始日期(即原始的locale),并设置 locale 参数 dmy 。正是这样做是依赖于平台的(请参阅?locales 。这是我将如何在Windows中执行的:

Another possibility is to use the original dates (i.e. in their original 'locale'), and set the locale argument in dmy. Exactly how this is done is platform dependent (see ?locales. Here is how I would do it in Windows:

dmy(date_english, locale = "English")
[1] "2006-09-04 UTC" "2006-10-27 UTC" "2007-01-08 UTC" "2007-01-28 UTC" "2007-01-05 UTC"

这篇关于日期转换而不指定格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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