为什么parse_date_time的mm / dd / yyyy格式结果不正确?

时间:2016-01-27 17:06:30

标签: r date

在尝试将此日期字符串转换为正确类的对象时,我不确定我在哪里出错。

class(StartDates$Start_Date)
[1] "character"
StartDates$Start_Date
 [1] "1/5/2015"   "1/12/2015"  "3/2/2015"   "4/13/2015"  "5/4/2015"   "6/1/2015"   "6/8/2015"   "6/29/2015"  "7/13/2015"  "8/31/2015"  "10/12/2015"
[12] "10/26/2015" "11/30/2015" "1/6/2014"   "1/13/2014"  "3/3/2014"   "4/14/2014"  "5/5/2014"   "6/2/2014"   "6/16/2014"  "6/30/2014"  "7/14/2014" 
[23] "9/2/2014"   "10/13/2014" "10/27/2014" "12/1/2014"  "9/28/2015"  "9/29/2014"  "1/4/2016"   "1/11/2016"  "2/29/2016"  "4/11/2016"  "5/2/2016"  
[34] "5/31/2016"  "6/6/2016"   "6/27/2016"  "7/11/2016"  "8/29/2016"  "10/10/2016" "10/24/2016" "11/28/2016"

parse_date_time(StartDates$Start_Date, c("mdy") )
 [1] "2015-05-01 00:00:00 UTC" "2015-12-01 00:00:00 UTC" "2015-02-03 00:00:00 UTC" "2016-01-04 00:00:00 UTC" "2015-04-05 00:00:00 UTC"
 [6] "2015-01-06 00:00:00 UTC" "2015-08-06 00:00:00 UTC" "2075-11-12 16:08:09 UTC" "2016-01-07 00:00:00 UTC" "2015-01-08 00:00:00 UTC"
[11] "2015-12-10 00:00:00 UTC" "2077-06-18 17:07:00 UTC" "2015-01-18 18:11:49 UTC" "2014-06-01 00:00:00 UTC" "2015-01-01 00:00:00 UTC"
[16] "2014-03-03 00:00:00 UTC" "1959-07-27 21:24:42 UTC" "2014-05-05 00:00:00 UTC" "2014-02-06 00:00:00 UTC" "2014-01-06 00:00:00 UTC"
[21] "2014-01-13 18:11:49 UTC" "1959-07-30 21:24:42 UTC" "2014-02-09 00:00:00 UTC" "2015-01-10 00:00:00 UTC" "2072-03-11 16:33:50 UTC"
[26] "2014-01-12 00:00:00 UTC" "2066-10-20 23:55:17 UTC" "2074-11-15 16:08:09 UTC" "2016-04-01 00:00:00 UTC" "2016-11-01 00:00:00 UTC"
[31] "2076-11-08 16:08:09 UTC" "2016-11-04 00:00:00 UTC" "2016-02-05 00:00:00 UTC" "2016-01-06 00:00:00 UTC" "2016-06-06 00:00:00 UTC"
[36] "2074-03-08 16:33:50 UTC" "2016-11-07 00:00:00 UTC" "2076-11-14 16:08:09 UTC" "2016-10-10 00:00:00 UTC" "2071-01-06 08:46:05 UTC"
[41] "2067-10-23 23:55:17 UTC"
> 

正如你所看到的,它正在以2067年和2074年的疯狂价值回归一些结果。

我确定我错过了一些明显的东西,但我找不到我的错误。我尝试了几种变化......

> parse_date_time(StartDates$Start_Date, "%m%d%Y")
 [1] "2015-05-01 00:00:00 UTC" "2015-12-01 00:00:00 UTC" "2015-02-03 00:00:00 UTC" "2016-01-04 00:00:00 UTC" "2015-04-05 00:00:00 UTC"
 [6] "2015-01-06 00:00:00 UTC" "2015-08-06 00:00:00 UTC" "2075-11-12 16:08:09 UTC" "2016-01-07 00:00:00 UTC" "2015-01-08 00:00:00 UTC"
[11] "2015-12-10 00:00:00 UTC" "2077-06-18 17:07:00 UTC" "2015-01-18 18:11:49 UTC" "2014-06-01 00:00:00 UTC" "2015-01-01 00:00:00 UTC"
[16] "2014-03-03 00:00:00 UTC" "1959-07-27 21:24:42 UTC" "2014-05-05 00:00:00 UTC" "2014-02-06 00:00:00 UTC" "2014-01-06 00:00:00 UTC"
[21] "2014-01-13 18:11:49 UTC" "1959-07-30 21:24:42 UTC" "2014-02-09 00:00:00 UTC" "2015-01-10 00:00:00 UTC" "2072-03-11 16:33:50 UTC"
[26] "2014-01-12 00:00:00 UTC" "2066-10-20 23:55:17 UTC" "2074-11-15 16:08:09 UTC" "2016-04-01 00:00:00 UTC" "2016-11-01 00:00:00 UTC"
[31] "2076-11-08 16:08:09 UTC" "2016-11-04 00:00:00 UTC" "2016-02-05 00:00:00 UTC" "2016-01-06 00:00:00 UTC" "2016-06-06 00:00:00 UTC"
[36] "2074-03-08 16:33:50 UTC" "2016-11-07 00:00:00 UTC" "2076-11-14 16:08:09 UTC" "2016-10-10 00:00:00 UTC" "2071-01-06 08:46:05 UTC"
[41] "2067-10-23 23:55:17 UTC"
> parse_date_time(StartDates$Start_Date, "dmY")
 [1] "2015-05-01 00:00:00 UTC" "2015-12-01 00:00:00 UTC" "2015-02-03 00:00:00 UTC" "2016-01-04 00:00:00 UTC" "2015-04-05 00:00:00 UTC"
 [6] "2015-01-06 00:00:00 UTC" "2015-08-06 00:00:00 UTC" "2075-11-12 16:08:09 UTC" "2016-01-07 00:00:00 UTC" "2015-01-08 00:00:00 UTC"
[11] "2015-12-10 00:00:00 UTC" "2077-06-18 17:07:00 UTC" "2015-01-18 18:11:49 UTC" "2014-06-01 00:00:00 UTC" "2015-01-01 00:00:00 UTC"
[16] "2014-03-03 00:00:00 UTC" "1959-07-27 21:24:42 UTC" "2014-05-05 00:00:00 UTC" "2014-02-06 00:00:00 UTC" "2014-01-06 00:00:00 UTC"
[21] "2014-01-13 18:11:49 UTC" "1959-07-30 21:24:42 UTC" "2014-02-09 00:00:00 UTC" "2015-01-10 00:00:00 UTC" "2072-03-11 16:33:50 UTC"
[26] "2014-01-12 00:00:00 UTC" "2066-10-20 23:55:17 UTC" "2074-11-15 16:08:09 UTC" "2016-04-01 00:00:00 UTC" "2016-11-01 00:00:00 UTC"
[31] "2076-11-08 16:08:09 UTC" "2016-11-04 00:00:00 UTC" "2016-02-05 00:00:00 UTC" "2016-01-06 00:00:00 UTC" "2016-06-06 00:00:00 UTC"
[36] "2074-03-08 16:33:50 UTC" "2016-11-07 00:00:00 UTC" "2076-11-14 16:08:09 UTC" "2016-10-10 00:00:00 UTC" "2071-01-06 08:46:05 UTC"
[41] "2067-10-23 23:55:17 UTC"
> parse_date_time(StartDates$Start_Date, "dmy")
 [1] "2015-05-01 00:00:00 UTC" "2015-12-01 00:00:00 UTC" "2015-02-03 00:00:00 UTC" "2016-01-04 00:00:00 UTC" "2015-04-05 00:00:00 UTC"
 [6] "2015-01-06 00:00:00 UTC" "2015-08-06 00:00:00 UTC" "2075-11-12 16:08:09 UTC" "2016-01-07 00:00:00 UTC" "2015-01-08 00:00:00 UTC"
[11] "2015-12-10 00:00:00 UTC" "2077-06-18 17:07:00 UTC" "2015-01-18 18:11:49 UTC" "2014-06-01 00:00:00 UTC" "2015-01-01 00:00:00 UTC"
[16] "2014-03-03 00:00:00 UTC" "1959-07-27 21:24:42 UTC" "2014-05-05 00:00:00 UTC" "2014-02-06 00:00:00 UTC" "2014-01-06 00:00:00 UTC"
[21] "2014-01-13 18:11:49 UTC" "1959-07-30 21:24:42 UTC" "2014-02-09 00:00:00 UTC" "2015-01-10 00:00:00 UTC" "2072-03-11 16:33:50 UTC"
[26] "2014-01-12 00:00:00 UTC" "2066-10-20 23:55:17 UTC" "2074-11-15 16:08:09 UTC" "2016-04-01 00:00:00 UTC" "2016-11-01 00:00:00 UTC"
[31] "2076-11-08 16:08:09 UTC" "2016-11-04 00:00:00 UTC" "2016-02-05 00:00:00 UTC" "2016-01-06 00:00:00 UTC" "2016-06-06 00:00:00 UTC"
[36] "2074-03-08 16:33:50 UTC" "2016-11-07 00:00:00 UTC" "2076-11-14 16:08:09 UTC" "2016-10-10 00:00:00 UTC" "2071-01-06 08:46:05 UTC"
[41] "2067-10-23 23:55:17 UTC"
> parse_date_time(StartDates$Start_Date, c("dmy") )
 [1] "2015-05-01 00:00:00 UTC" "2015-12-01 00:00:00 UTC" "2015-02-03 00:00:00 UTC" "2016-01-04 00:00:00 UTC" "2015-04-05 00:00:00 UTC"
 [6] "2015-01-06 00:00:00 UTC" "2015-08-06 00:00:00 UTC" "2075-11-12 16:08:09 UTC" "2016-01-07 00:00:00 UTC" "2015-01-08 00:00:00 UTC"
[11] "2015-12-10 00:00:00 UTC" "2077-06-18 17:07:00 UTC" "2015-01-18 18:11:49 UTC" "2014-06-01 00:00:00 UTC" "2015-01-01 00:00:00 UTC"
[16] "2014-03-03 00:00:00 UTC" "1959-07-27 21:24:42 UTC" "2014-05-05 00:00:00 UTC" "2014-02-06 00:00:00 UTC" "2014-01-06 00:00:00 UTC"
[21] "2014-01-13 18:11:49 UTC" "1959-07-30 21:24:42 UTC" "2014-02-09 00:00:00 UTC" "2015-01-10 00:00:00 UTC" "2072-03-11 16:33:50 UTC"
[26] "2014-01-12 00:00:00 UTC" "2066-10-20 23:55:17 UTC" "2074-11-15 16:08:09 UTC" "2016-04-01 00:00:00 UTC" "2016-11-01 00:00:00 UTC"
[31] "2076-11-08 16:08:09 UTC" "2016-11-04 00:00:00 UTC" "2016-02-05 00:00:00 UTC" "2016-01-06 00:00:00 UTC" "2016-06-06 00:00:00 UTC"
[36] "2074-03-08 16:33:50 UTC" "2016-11-07 00:00:00 UTC" "2076-11-14 16:08:09 UTC" "2016-10-10 00:00:00 UTC" "2071-01-06 08:46:05 UTC"
[41] "2067-10-23 23:55:17 UTC"
> ymd(StartDates$Start_Date)
 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Warning message:
All formats failed to parse. No formats found. 

这是dput:

dput(StartDates$Start_Date)
c("1/5/2015", "1/12/2015", "3/2/2015", "4/13/2015", "5/4/2015", 
"6/1/2015", "6/8/2015", "6/29/2015", "7/13/2015", "8/31/2015", 
"10/12/2015", "10/26/2015", "11/30/2015", "1/6/2014", "1/13/2014", 
"3/3/2014", "4/14/2014", "5/5/2014", "6/2/2014", "6/16/2014", 
"6/30/2014", "7/14/2014", "9/2/2014", "10/13/2014", "10/27/2014", 
"12/1/2014", "9/28/2015", "9/29/2014", "1/4/2016", "1/11/2016", 
"2/29/2016", "4/11/2016", "5/2/2016", "5/31/2016", "6/6/2016", 
"6/27/2016", "7/11/2016", "8/29/2016", "10/10/2016", "10/24/2016", 
"11/28/2016")

1 个答案:

答案 0 :(得分:2)

从基础包中尝试as.Date ....您需要指定编码日期的格式,以便正确解析和转换。

d
 [1] "1/5/2015"   "1/12/2015"  "3/2/2015"   "4/13/2015"  "5/4/2015"   "6/1/2015"  
 [7] "6/8/2015"   "6/29/2015"  "7/13/2015"  "8/31/2015"  "10/12/2015" "10/26/2015"
[13] "11/30/2015" "1/6/2014"   "1/13/2014"  "3/3/2014"   "4/14/2014"  "5/5/2014"  
[19] "6/2/2014"   "6/16/2014"  "6/30/2014"  "7/14/2014"  "9/2/2014"   "10/13/2014"
[25] "10/27/2014" "12/1/2014"  "9/28/2015"  "9/29/2014"  "1/4/2016"   "1/11/2016" 
[31] "2/29/2016"  "4/11/2016"  "5/2/2016"   "5/31/2016"  "6/6/2016"   "6/27/2016" 
[37] "7/11/2016"  "8/29/2016"  "10/10/2016" "10/24/2016" "11/28/2016"
as.Date(d, format = '%m/%d/%Y')
 [1] "2015-01-05" "2015-01-12" "2015-03-02" "2015-04-13" "2015-05-04" "2015-06-01"
 [7] "2015-06-08" "2015-06-29" "2015-07-13" "2015-08-31" "2015-10-12" "2015-10-26"
[13] "2015-11-30" "2014-01-06" "2014-01-13" "2014-03-03" "2014-04-14" "2014-05-05"
[19] "2014-06-02" "2014-06-16" "2014-06-30" "2014-07-14" "2014-09-02" "2014-10-13"
[25] "2014-10-27" "2014-12-01" "2015-09-28" "2014-09-29" "2016-01-04" "2016-01-11"
[31] "2016-02-29" "2016-04-11" "2016-05-02" "2016-05-31" "2016-06-06" "2016-06-27"
[37] "2016-07-11" "2016-08-29" "2016-10-10" "2016-10-24" "2016-11-28"