将整数格式的日期转换为日期的双精度格式 [英] Converting integer format date to double format of date

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

问题描述

我在数据框中具有以下格式的日期格式:

I have date format in following format in a data frame:

Jan-85
Apr-99
1-Nov
Feb-96

当我看到typeof(df$col)时,得到的答案是整数".

When I see the typeof(df$col) I get the answer as "integer".

实际上,当我看到excel格式时,它是m/d/yyyy格式.我试图将其转换为R中的日期格式.我所有的努力都产生了NA.

Actually when I see the format in excel it is in m/d/yyyy format. I was trying to convert this to date format in R. All my efforts yielded NA.

我尝试了parse_date_time函数.我尝试了as.dateas.character.我尝试了as.POSIXct,但是一切都让我不满意.

I tried parse_date_time function. I tried as.date along with as.character. I tried as.POSIXct but everything is giving me NA.

我的尝试如下,一切都失败了:

My trials were as follows and everything was a failure:

as.Date.numeric(df$col,"m%d%Y")

transform(df$col, as.Date(as.character(df$col), "%m%d%Y"))

as.Date(df$col,"m%d%Y")

as.POSIXct.numeric(as.character(loan_new$issue_d), format="%Y%m%d")

as.POSIXct.date(as.character(df$col), format="%Y%m%d")

mdy(df$col)

parse_date_time(df$col,c("mdy"))

如何将其转换为日期格式?我已经将lubridate包用于parse_date_time和mdy包.

How can I convert this to date format? I have used lubridate package for parse_date_time and mdy package.

dput输出低于

Label <- factor(c("Apr-08", 
"Apr-09", "Apr-10", "Apr-11", "Aug-07", "Aug-08", "Aug-09", "Aug-10", 
"Aug-11", "Dec-07", "Dec-08", "Dec-09", "Dec-10", "Dec-11", "Feb-08", 
"Feb-09", "Feb-10", "Feb-11", "Jan-08", "Jan-09", "Jan-10", "Jan-11", 
"Jul-07", "Jul-08", "Jul-09", "Jul-10", "Jul-11", "Jun-07", "Jun-08", 
"Jun-09", "Jun-10", "Jun-11", "Mar-08", "Mar-09", "Mar-10", "Mar-11", 
"May-08", "May-09", "May-10", "May-11", "Nov-07", "Nov-08", "Nov-09", 
"Nov-10", "Nov-11", "Oct-07", "Oct-08", "Oct-09", "Oct-10", "Oct-11", 
"Sep-07", "Sep-08", "Sep-09", "Sep-10", "Sep-11"))

推荐答案

NA通常是错误指定格式时得到的内容.你是做什么的.就是说,如果您的数据确实像您给出的第一个示例,则不可能简单地将其转换为日期.您有两种不同的格式,一种是月-年,另一种是月-月.

NA is typically what you get when you misspecify the format. Which is what you do. That said, if your data is really looking like the first example you gave, it's impossible to simply convert this to a date. You have two different formats, one being month-year and the other day-month.

如果更新的日期(即Dec-11)是正确的格式,则可以使用as.Dateformat参数,如下所示:

If your updated date (i.e. Dec-11) is the correct format, then you use the format argument of as.Date like this:

date <- "Dec-11"
as.Date(date, format = "%b-%d")
# [1] "2017-12-11"

或者在您的示例数据上:

Or on your example data:

as.Date(Label, format = "%b-%d")
# [1] "2017-04-08" "2017-04-09" "2017-04-10" "2017-04-11" "2017-08-07" "2017-08-08"
# [7] "2017-08-09" "2017-08-10" "2017-08-11" "2017-12-07" "2017-12-08" "2017-12-09"

如果要转换Jan-85之类的内容,则必须确定该日期应在每月的哪一天.假设我们只是每个月的第一天,那么您可以:

If you want to convert something like Jan-85, you have to decide which day of the month that date should have. Say we just take the first of each month, then you can do:

x <- "Jan-85"
xd <- paste0("1-",x)
as.Date(xd, "%d-%b-%y")
# [1] "1985-01-01"

有关格式代码的更多信息,请参见?strptime

More information on the format codes can be found on ?strptime

请注意,R会自动将今年添加为年份.必须填写,否则无法指定日期.如果您没有一个月中的某天(例如像Jan-85),则不可能转换为日期,因为基础POSIX算法没有所有必要的信息.

Note that R will automatically add this year as the year. It has to, otherwise it can't specify the date. In case you do not have a day of the month (eg like Jan-85), conversion to a date is impossible because the underlying POSIX algorithms don't have all necessary information.

还请记住,这仅在您的语言环境设置为英语时才有效.否则,您的操作系统很有可能无法正确识别月份缩写.为此,例如:

Also keep in mind that this only works when your locale is set to english. Otherwise you have a big chance your OS won't recognize the month abbreviations correctly. To do so, do eg:

Sys.setlocale(category = "LC_TIME", locale = "English_United Kingdom")

如果有必要,您以后可以将其设置回原始设置,或者重新启动R会话以重置语言环境设置.

You can later set it back to the original one if you must, or restart your R session to reset the locale settings.

注释:请仔细检查哪些区域设置符号对您的操作系统有效.上面的示例可在Windows上运行,但不能在Linux或Mac上得到保证.

note: Please check carefully which locale notations are valid for your OS. The above example works on Windows, but is not guaranteed on either Linux or Mac.

为什么会看到整数

这些字符串值是整数类型的事实是由于R读取数据帧时R自动将字符向量转换为因子.所以typeof()返回integer,因为那是一个因素的内部表示.

The fact that these string values are of integer type, is due to the fact that R automatically convert character vectors to factors when reading in a data frame. So typeof() returns integer because that's the internal representation of a factor.

这篇关于将整数格式的日期转换为日期的双精度格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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