简体   繁体   English

将非标准日期格式字符串(“April-20”)转换为日期对象 R

[英]Converting non-standard date format strings (“April-20”) to date objects R

I have a vector of date strings in the form month_name-2_digit_year ie我有一个格式为 month_name-2_digit_year 的日期字符串向量,即

a = rbind("April-21", "March-21", "February-21", "January-21") 

I'm trying to convert that vector into a vector of date objects.我正在尝试将该向量转换为日期对象的向量。 I'm aware this question is very similar to this: Convert non-standard date format to date in R posted some years ago, but unfortunately, it has not answered my question.我知道这个问题与此非常相似: Convert non-standard date format to date in R几年前发布,但不幸的是,它没有回答我的问题。

I have tried the following as.Date() calls to do this, but it just returns a vector of NA.我尝试了以下 as.Date() 调用来执行此操作,但它只返回 NA 的向量。 Ie IE

b = as.Date(a, format = "%B-%y")
b = as.Date(a, format = "%B%y")
b = as.Date(a, "%B-%y")
b = as.Date(a, "%B%y")

I'm also attempted to do it using the convertToDate function from the openxlsx package:我还尝试使用 openxlsx 包中的 convertToDate 函数来做到这一点:

b = convertToDate(a, format = "%B-%y") 

I have also tried all the above but using a single character string rather than a vector, but that produced the same issue.我也尝试了以上所有方法,但使用的是单个字符串而不是向量,但这产生了同样的问题。

I'm a little lost as to why this isn't working, as this format has worked in reverse earlier in my script (that is, I had a date object already in dd-mm-yyyy format and converted it to month_name-yy using %B-%y).我有点不明白为什么这不起作用,因为这种格式在我的脚本早期反向工作(也就是说,我已经有一个 dd-mm-yyyy 格式的日期对象并将其转换为 month_name-yy使用 %B-%y)。 Is there another way to go from string to date when the string is a non-standard (anything other than dd-mm-yyy or mm-dd-yy if you're in the US) date format?当字符串是非标准(除 dd-mm-yyy 或 mm-dd-yy 之外的任何其他内容,如果您在美国)日期格式时,是否有另一种方法可以从字符串到日期?

For the record my R locales are all UK and english.根据记录,我的 R 语言环境都是英国和英语。

Thanks in advance.提前致谢。

A Date must have all three of day, month and year.日期必须包含日、月和年这三者。 Convert to yearmon class which requires only month and year and then to Date as in (1) and (2) below or add the day as in (3).转换为仅需要月份和年份的 yearmon 类,然后转换为 Date,如下面的 (1) 和 (2) 或添加日期,如 (3)。

(1) and (3) give first of month and (2) gives the end of the month. (1) 和 (3) 给出月初,(2) 给出月底。

(3) uses only functions from base R. (3) 仅使用来自基础 R 的函数。

Also consider not converting to Date at all but just use yearmon objects instead since they directly represent a year and month which is what the input represents.还要考虑根本不转换为 Date 而是只使用 yearmon 对象,因为它们直接代表输入所代表的年份和月份。

library(zoo)

# test input
a <- c("April-21", "March-21", "February-21", "January-21") 

# 1
as.Date(as.yearmon(a, "%B-%y"))
## [1] "2021-04-01" "2021-03-01" "2021-02-01" "2021-01-01"

# 2
as.Date(as.yearmon(a, "%B-%y"), frac = 1)
## [1] "2021-04-30" "2021-03-31" "2021-02-28" "2021-01-31"

# 3
as.Date(paste(1, a), "%d %B-%y")
## [1] "2021-04-01" "2021-03-01" "2021-02-01" "2021-01-01"

In addition to zoo, which @G.除了动物园,@G。 Grothendieck mentioned, you can also use clock or lubridate. Grothendieck 提到,你也可以使用clock 或lubridate。

clock supports a variable precision calendar type called year_month_day . clock 支持称为year_month_day的可变精度日历类型。 In this case you'd want "month" precision, then you can set the day to whatever you'd like and convert back to Date.在这种情况下,您需要"month"精度,然后您可以将日期设置为您想要的任何日期并转换回日期。

library(clock)

x <- c("April-21", "March-21", "February-21", "January-21") 

ymd <- year_month_day_parse(x, format = "%B-%y", precision = "month")
ymd
#> <year_month_day<month>[4]>
#> [1] "2021-04" "2021-03" "2021-02" "2021-01"

# First of month
as.Date(set_day(ymd, 1))
#> [1] "2021-04-01" "2021-03-01" "2021-02-01" "2021-01-01"

# End of month
as.Date(set_day(ymd, "last"))
#> [1] "2021-04-30" "2021-03-31" "2021-02-28" "2021-01-31"

The simplest solution may be to use lubridate::my() , which parses strings in the order of "month then year".最简单的解决方案可能是使用lubridate::my() ,它按“月然后年”的顺序解析字符串。 That assumes that you want the first day of the month, which may or may not be correct for you.这假设您想要一个月的第一天,这对您来说可能正确也可能不正确。

library(lubridate)

x <- c("April-21", "March-21", "February-21", "January-21") 

# Assumes first of month
my(x)
#> [1] "2021-04-01" "2021-03-01" "2021-02-01" "2021-01-01"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM