简体   繁体   English

日期功能不起作用

[英]as.Date function not working

I have a huge data set I am working with. 我有一个庞大的数据集正在使用。 Some of the months are in the format 01/01/2010 and others are 1/1/2010 . 某些月份的格式为01/01/2010 ,其他月份的格式为1/1/2010

When I run as.Date(Dates, format="%y/%d/%m") all of the latter dates change the year to 2020. What is going on here? 当我运行as.Date(Dates, format="%y/%d/%m")所有后一个日期都会更改为2020年。这是怎么回事?

Your format statement is not correct. 您的格式声明不正确。 Try this: 尝试这个:

d1 <- "01/01/2010"
d2 <- "1/1/2010"
> as.Date(d1, format='%d/%m/%Y')
#[1] "2010-01-01"
> as.Date(d2, format='%d/%m/%Y')
#[1] "2010-01-01"

For dates with different formats of the year, the lubridate package can be used: 对于年份格式不同的日期,可以使用lubridate软件包:

library(lubridate)
d1 <- "1/1/10"
d2 <- "01/01/2010"
parse_date_time(d1, "dmy")
#[1] "2010-01-01 UTC"
parse_date_time(d2, "dmy")
#[1] "2010-01-01 UTC"

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

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