修复日期向量

时间:2018-03-05 07:12:31

标签: r matlab date xts lubridate

如果您查看以下日期向量,则日期不一致。 如果你看,你会看到前8个日期的格式为dd / m / yr,接下来的8个日期的格式为mm / dd / yr。

我有5000个日期不断更改格式。原始数据是这样的。我想格式化整个5000个单元格,使其在一个向量中具有相同的格式。我想用这个向量进行预测。

另外,你能帮助我预测在给定日期结束时还有50个日期吗? 全部在R或MATLAB中。

19/9/1997 22/9/1997 23/9/1997 24/9/1997 25/9/1997 26/9/1997 29/9/1997
30/9/1997 10/01/1997 10/02/1997 10/03/1997 10/06/1997 10/07/1997
10/08/1997 10/09/1997 10/10/1997 13/10/1997 14/10/1997 15/10/1997
16/10/1997 17/10/1997 20/10/1997 21/10/1997 22/10/1997 23/10/1997
24/10/1997 27/10/1997 28/10/1997 29/10/1997 30/10/1997 31/10/1997
11/03/1997 11/04/1997 11/05/1997 11/06/1997 11/07/1997 11/10/1997
11/11/1997 11/12/1997 13/11/1997 14/11/1997 17/11/1997 18/11/1997
19/11/1997 20/11/1997 21/11/1997 24/11/1997 25/11/1997 26/11/1997
27/11/1997 28/11/1997 12/01/1997 12/02/1997 12/03/1997 12/04/1997
12/05/1997 12/08/1997 12/09/1997 12/10/1997 12/11/1997 12/12/1997
15/12/1997 16/12/1997 17/12/1997 18/12/1997 19/12/1997 22/12/1997
23/12/1997 24/12/1997 25/12/1997 26/12/1997 29/12/1997 30/12/1997
31/12/1997 1/01/1998 1/02/1998 1/05/1998 1/06/1998 1/07/1998 1/08/1998
1/09/1998 1/12/1998 13/1/1998 14/1/1998 15/1/1998 16/1/1998 19/1/1998
20/1/1998 21/1/1998 22/1/1998 23/1/1998 26/1/1998 27/1/1998 28/1/1998
29/1/1998 30/1/1998

Chinsoon 12提出以下更新......我试过......但仍然出错......

> library(readxl)
> Dates <- read_excel("C:/Users/Mihan/Dropbox/Mardi Meetings/Dataset/Dates.xlsx")
> x<-Dates[,1]
> 
> ans <- Reduce(function(prev, curr) {
+   f1 <- as.Date(curr, "%d/%m/%Y")
+   f2 <- as.Date(curr, "%m/%d/%Y")
+   if (is.na(f1)) return(f2)
+   if (is.na(f2)) return(f1)
+   if (prev < f1 && prev < f2) return(min(f1, f2))
+   if (prev < f1) return(f1)
+   if (prev < f2) return(f2)
+ }, x[-1], init=as.Date(x[1], "%d/%m/%Y"), accumulate=TRUE)
Error in as.Date.default(x[1], "%d/%m/%Y") : 
  do not know how to convert 'x[1]' to class “Date”
> 
> as.Date(ans, origin="1970-01-01")
Error in as.Date(ans, origin = "1970-01-01") : object 'ans' not found
thnx chinsoon ...见

  

头(x)的

33274
33302
33394
33424
33455
33486
33516
13/5/1991
14/5/1991
15/5/1991
16/5/1991
17/5/1991
20/5/1991
21/5/1991
22/5/1991
23/5/1991
24/5/1991
27/5/1991
28/5/1991
29/5/1991
30/5/1991
31/5/1991
33303
33334
33364
33395
33425
33517
33548
33578
13/6/1991
14/6/1991
17/6/1991
18/6/1991
19/6/1991
20/6/1991
21/6/1991

它们按时间顺序排列......格式如你所见扭曲.... mm / dd / yy是其他东西......但是dd / mm / yy还可以

PREM请稍等一下

> rm (list = ls(all=TRUE))
> graphics.off()
> install.packages("readxl")
Installing package into ‘C:/Users/Mihan/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/readxl_1.0.0.zip'
Content type 'application/zip' length 1457710 bytes (1.4 MB)
downloaded 1.4 MB

package ‘readxl’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\Mihan\AppData\Local\Temp\Rtmpo7uHvQ\downloaded_packages
> install.packages("lubridate")
Installing package into ‘C:/Users/Mihan/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)

  There is a binary version available but the source version is later:
          binary source needs_compilation
lubridate  1.7.2  1.7.3              TRUE

  Binaries will be installed
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/lubridate_1.7.2.zip'
Content type 'application/zip' length 1366433 bytes (1.3 MB)
downloaded 1.3 MB

package ‘lubridate’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\Mihan\AppData\Local\Temp\Rtmpo7uHvQ\downloaded_packages
> install.packages("dplyr")
Installing package into ‘C:/Users/Mihan/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/dplyr_0.7.4.zip'
Content type 'application/zip' length 2890413 bytes (2.8 MB)
downloaded 2.8 MB

package ‘dplyr’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\Mihan\AppData\Local\Temp\Rtmpo7uHvQ\downloaded_packages
> library(readxl)
> library(lubridate)

Attaching package: ‘lubridate’

The following object is masked from ‘package:base’:

    date

> library(dplyr)

Attaching package: ‘dplyr’

The following objects are masked from ‘package:lubridate’:

    intersect, setdiff, union

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

> Dates <- read_excel("C:/Users/Mihan/Dropbox/Mardi Meetings/Dataset/Dates.xlsx")
> #x<-Dates[,1]
> #df<-structure(list(v1=x), .Names = "V1", class = "data.frame", row.names = c(NA, 
> #                                                                             -6523L))
> df <- data.frame(V1 = Dates[,1])
> #df$V1 <- df$V1$Dates
> df %>% 
+   mutate(converted = ifelse(nchar(V1) >= 8, 
+                             as.character(parse_date_time(V1, orders = "%d/%m/%Y")), 
+                             as.character(parse_date_time(as.Date(as.numeric(V1), origin="1899-12-30"), orders = "%Y-%d-%m"))))
Error in mutate_impl(.data, dots) : 
  Evaluation error: object 'V1' not found.
dput(dates)
"19/7/1994", "20/7/1994", "21/7/1994", "22/7/1994", "25/7/1994", 
"26/7/1994", "27/7/1994", "28/7/1994", "29/7/1994", "34342", 
"34373", "34401", "34432", "34462", "34554", "34585", "34615", 
"34646", "34676", "15/8/1994", "16/8/1994", "17/8/1994", "18/8/1994", 
"19/8/1994", "22/8/1994", "23/8/1994", "24/8/1994", "25/8/1994", 
"26/8/1994", "29/8/1994", "30/8/1994", "31/8/1994", "34343", 
"34374", "34463", "34494", "34524", "34555", "34586", "34677", 
"13/9/1994", "14/9/1994", "15/9/1994", "16/9/1994", "19/9/1994", 
"20/9/1994", "21/9/1994", "22/9/1994", "23/9/1994", "26/9/1994", 
"27/9/1994", "28/9/1994", "29/9/1994", "30/9/1994", "34403", 
"34434", "34464", "34495", "34525", "34617", "34648", "34678", 
"13/10/1994", "14/10/1994", "17/10/1994", "18/10/1994", "19/10/1994", 
"20/10/1994", "21/10/1994", "24/10/1994", "25/10/1994", "26/10/1994", 
"27/10/1994", "28/10/1994", "31/10/1994", "34345", "34376", "34404", 
"34435", "34526", "34557", "34588", "34618", "34649", "14/11/1994", 
"15/11/1994", "16/11/1994", "17/11/1994", "18/11/1994", "21/11/1994", 
"22/11/1994", "23/11/1994", "24/11/1994", "25/11/1994", "28/11/1994", 
"29/11/1994", "30/11/1994", "34346", "34377", "34466", "34497", 
"34527", "34558", "34589", "34680", "13/12/1994", "14/12/1994", 
"15/12/1994", "16/12/1994", "19/12/1994", "20/12/1994", "21/12/1994", 
"22/12/1994", "23/12/1994", "26/12/1994", "27/12/1994", "28/12/1994", 
"29/12/1994", "30/12/1994", "34731", "34759", "34790", "34820", 
"34851", "34943", "34973", "35004", "35034", "13/1/1995", "16/1/1995", 
"17/1/1995", "18/1/1995", "19/1/1995", "20/1/1995", "23/1/1995", 
"24/1/1995", "25/1/1995", "26/1/1995", "27/1/1995", "30/1/1995", 
"31/1/1995", "34701", "34732", "34760", "34852", "34882", "34913", 
"34944", "34974", "13/2/1995", "14/2/1995", "15/2/1995", "16/2/1995", 
"17/2/1995", "20/2/1995", "21/2/1995", "22/2/1995", "23/2/1995", 
"24/2/1995", "27/2/1995", "28/2/1995", "34702", "34733", "34761", 
"34853", "34883", "34914", "34945", "34975", "13/3/1995", "14/3/1995", 
"15/3/1995", "16/3/1995", "17/3/1995", "20/3/1995", "21/3/1995", 
"22/3/1995", "23/3/1995", "24/3/1995", "27/3/1995", "28/3/1995", 
"29/3/1995", "30/3/1995", "31/3/1995", "34762", "34793", "34823", 
"34854", "34884", "34976", "35007", "35037", "13/4/1995", "14/4/1995", 
"17/4/1995", "18/4/1995", "19/4/1995", "20/4/1995", "21/4/1995", 
"24/4/1995", "25/4/1995", "26/4/1995", "27/4/1995", "28/4/1995", 
"34704", "34735", "34763", "34794", "34824", "34916", "34947", 
"34977", "35008", "35038", "15/5/1995", "16/5/1995", "17/5/1995", 
"18/5/1995", "19/5/1995", "22/5/1995", "23/5/1995", "24/5/1995", 
"25/5/1995", "26/5/1995", "29/5/1995", "30/5/1995", "31/5/1995", 
"34705", "34736", "34825", "34856", "34886", "34917", "34948", 
"35039", "13/6/1995", "14/6/1995", "15/6/1995", "16/6/1995", 
"19/6/1995", "20/6/1995", "21/6/1995", "22/6/1995", "23/6/1995", 
"26/6/1995", "27/6/1995", "28/6/1995", "29/6/1995", "30/6/1995", 
"34765", "34796", "34826", "34857", "34887", "34979", "35010", 
"35040", "13/7/1995", "14/7/1995", "17/7/1995", "18/7/1995", 
"19/7/1995", "20/7/1995", "21/7/1995", "24/7/1995", "25/7/1995", 
"26/7/1995", "27/7/1995", "28/7/1995", "31/7/1995", "34707", 
"34738", "34766", "34797", "34888", "34919", "34950", "34980", 
"35011", "14/8/1995", "15/8/1995", "16/8/1995", "17/8/1995", 
"18/8/1995", "21/8/1995", "22/8/1995", "23/8/1995", "24/8/1995", 
"25/8/1995", "28/8/1995", "29/8/1995", "30/8/1995", "31/8/1995", 
"34708", "34798", "34828", "34859", "34889", "34920", "35012", 
"35042", "13/9/1995", "14/9/1995", "15/9/1995", "18/9/1995", 
"19/9/1995", "20/9/1995", "21/9/1995", "22/9/1995", "25/9/1995", 
"26/9/1995", "27/9/1995", "28/9/1995", "29/9/1995", "34740", 
"34768", "34799", "34829", "34860", "34952", "34982", "35013", 
"35043", "13/10/1995", "16/10/1995", "17/10/1995", "18/10/1995", 
"19/10/1995", "20/10/1995", "23/10/1995", "24/10/1995", "25/10/1995", 
"26/10/1995", "27/10/1995", "30/10/1995", "31/10/1995", "34710", 
"34741", "34769", "34861", "34891", "34922", "34953", "34983", 
"13/11/1995", "14/11/1995", "15/11/1995", "16/11/1995", "17/11/1995", 
"20/11/1995", "21/11/1995", "22/11/1995", "23/11/1995", "24/11/1995", 
"27/11/1995", "28/11/1995", "29/11/1995", "30/11/1995", "34711", 
"34801", "34831", "34862", "34892", "34923", "35015", "35045", 
"13/12/1995", "14/12/1995", "15/12/1995", "18/12/1995", "19/12/1995", 
"20/12/1995", "21/12/1995", "22/12/1995", "25/12/1995", "26/12/1995", 
"27/12/1995", "28/12/1995", "29/12/1995", "35065", "35096", "35125", 
"35156", "35186", "35278", "35309", "35339", "35370", "35400", 
"15/1/1996", "16/1/1996", "17/1/1996", "18/1/1996", "19/1/1996", 
"22/1/1996", "23/1/1996", "24/1/1996", "25/1/1996", "26/1/1996", 
"29/1/1996", "30/1/1996", "31/1/1996", "35066", "35097", "35187", 
"35218", "35248", "35279", "35310", "35401", "13/2/1996", "14/2/1996", 
"15/2/1996", "16/2/1996", "19/2/1996", "20/2/1996", "21/2/1996", 
"22/2/1996", "23/2/1996", "26/2/1996", "27/2/1996", "28/2/1996", 
"29/2/1996", "35067", "35158", "35188", "35219", "35249", "35280", 
"35372", "35402", "13/3/1996", "14/3/1996", "15/3/1996", "18/3/1996", 
"19/3/1996", "20/3/1996", "21/3/1996", "22/3/1996", "25/3/1996", 
"26/3/1996", "27/3/1996", "28/3/1996", "29/3/1996", "35068", 
"35099", "35128", "35159", "35189", "35281", "35312", "35342", 
"35373", "35403", "15/4/1996", "16/4/1996", "17/4/1996", "18/4/1996", 
"19/4/1996", "22/4/1996", "23/4/1996", "24/4/1996", "25/4/1996", 
"26/4/1996", "29/4/1996", "30/4/1996", "35069", "35100", "35129", 
"35221", "35251", "35282", "35313", "35343", "13/5/1996", "14/5/1996", 
"15/5/1996", "16/5/1996", "17/5/1996", "20/5/1996", "21/5/1996", 
"22/5/1996", "23/5/1996", "24/5/1996", "27/5/1996", "28/5/1996", 
"29/5/1996", "30/5/1996", "31/5/1996", "35130", "35161", "35191", 
"35222", "35252", "35344", "35375", "35405", "13/6/1996", "14/6/1996", 
"17/6/1996", "18/6/1996", "19/6/1996", "20/6/1996", "21/6/1996", 
"24/6/1996", "25/6/1996", "26/6/1996", "27/6/1996", "28/6/1996", 
"35071", "35102", "35131", "35162", "35192", "35284", "35315", 
"35345", "35376", "35406", "15/7/1996", "16/7/1996", "17/7/1996", 
"18/7/1996", "19/7/1996", "22/7/1996", "23/7/1996", "24/7/1996", 
"25/7/1996", "26/7/1996", "29/7/1996", "30/7/1996", "31/7/1996", 
"35072", "35103", "35193", "35224", "35254", "35285", "35316", 
"35407", "13/8/1996", "14/8/1996", "15/8/1996", "16/8/1996", 
"19/8/1996", "20/8/1996", "21/8/1996", "22/8/1996", "23/8/1996", 
"26/8/1996", "27/8/1996", "28/8/1996", "29/8/1996", "30/8/1996", 
"35104", "35133", "35164", "35194", "35225", "35317", "35347", 
"35378", "35408", "13/9/1996", "16/9/1996", "17/9/1996", "18/9/1996", 
"19/9/1996", "20/9/1996", "23/9/1996", "24/9/1996", "25/9/1996", 
"26/9/1996", "27/9/1996", "30/9/1996", "35074", "35105", "35134", 
"35165", "35256", "35287", "35318", "35348", "35379", "14/10/1996", 
"15/10/1996", "16/10/1996", "17/10/1996", "18/10/1996", "21/10/1996", 
"22/10/1996", "23/10/1996", "24/10/1996", "25/10/1996", "28/10/1996", 
"29/10/1996", "30/10/1996", "31/10/1996", "35075", "35166", "35196", 
"35227", "35257", "35288", "35380", "35410", "13/11/1996", "14/11/1996", 
"15/11/1996", "18/11/1996", "19/11/1996", "20/11/1996", "21/11/1996", 
"22/11/1996", "25/11/1996", "26/11/1996", "27/11/1996", "28/11/1996", 
"29/11/1996", "35107", "35136", "35167", "35197", "35228", "35320", 
"35350", "35381", "35411", "13/12/1996", "16/12/1996", "17/12/1996", 
"18/12/1996", "19/12/1996", "20/12/1996", "23/12/1996", "24/12/1996", 
"25/12/1996", "26/12/1996", "27/12/1996", "30/12/1996", "31/12/1996", 
"35431", "35462", "35490", "35582", "35612", "35643", "35674", 
"35704", "13/1/1997", "14/1/1997", "15/1/1997", "16/1/1997", 
"17/1/1997", "20/1/1997", "21/1/1997", "22/1/1997", "23/1/1997", 
"24/1/1997", "27/1/1997", "28/1/1997", "29/1/1997", "30/1/1997", 
"31/1/1997", "35491", "35522", "35552", "35583", "35613", "35705", 
"35736", "35766", "13/2/1997", "14/2/1997", "17/2/1997", "18/2/1997", 
"19/2/1997", "20/2/1997", "21/2/1997", "24/2/1997", "25/2/1997", 
"26/2/1997", "27/2/1997", "28/2/1997", "35492", "35523", "35553", 
"35584", "35614", "35706", "35737", "35767", "13/3/1997", "14/3/1997", 
"17/3/1997", "18/3/1997", "19/3/1997", "20/3/1997", "21/3/1997", 
"24/3/1997", "25/3/1997", "26/3/1997", "27/3/1997", "28/3/1997", 
"31/3/1997", "35434", "35465", "35493", "35524", "35615", "35646", 
"35677", "35707", "35738", "14/4/1997", "15/4/1997", "16/4/1997", 
"17/4/1997", "18/4/1997", "21/4/1997", "22/4/1997", "23/4/1997", 
"24/4/1997", "25/4/1997", "28/4/1997", "29/4/1997", "30/4/1997", 
"35435", "35466", "35555", "35586", "35616", "35647", "35678", 
"35769", "13/5/1997", "14/5/1997", "15/5/1997", "16/5/1997", 
"19/5/1997", "20/5/1997", "21/5/1997", "22/5/1997", "23/5/1997", 
"26/5/1997", "27/5/1997", "28/5/1997", "29/5/1997", "30/5/1997", 
"35467", "35495", "35526", "35556", "35587", "35679", "35709", 
"35740", "35770", "13/6/1997", "16/6/1997", "17/6/1997", "18/6/1997", 
"19/6/1997", "20/6/1997", "23/6/1997", "24/6/1997", "25/6/1997", 
"26/6/1997", "27/6/1997", "30/6/1997", "35437", "35468", "35496", 
"35527", "35618", "35649", "35680", "35710", "35741", "14/7/1997", 
"15/7/1997", "16/7/1997", "17/7/1997", "18/7/1997", "21/7/1997", 
"22/7/1997", "23/7/1997", "24/7/1997", "25/7/1997", "28/7/1997", 
"29/7/1997", "30/7/1997", "31/7/1997", "35438", "35528", "35558", 
"35589", "35619", "35650", "35742", "35772", "13/8/1997", "14/8/1997", 
"15/8/1997", "18/8/1997", "19/8/1997", "20/8/1997", "21/8/1997", 
"22/8/1997", "25/8/1997", "26/8/1997", "27/8/1997", "28/8/1997", 
"29/8/1997", "35439", "35470", "35498", "35529", "35559", "35651", 
"35682", "35712", "35743", "35773", "15/9/1997", "16/9/1997", 
"17/9/1997", "18/9/1997", "19/9/1997", "22/9/1997", "23/9/1997", 
"24/9/1997", "25/9/1997", "26/9/1997", "29/9/1997", "30/9/1997", 
"35440", "35471", "35499", "35591", "35621", "35652", "35683", 
"35713", "13/10/1997", "14/10/1997", "15/10/1997", "16/10/1997", 
"17/10/1997", "20/10/1997", "21/10/1997", "22/10/1997", "23/10/1997", 
"24/10/1997", "27/10/1997", "28/10/1997", "29/10/1997", "30/10/1997", 
"31/10/1997", "35500", "35531", "35561", "35592", "35622", "35714", 
"35745", "35775", "13/11/1997", "14/11/1997", "17/11/1997", "18/11/1997", 
"19/11/1997", "20/11/1997", "21/11/1997", "24/11/1997", "25/11/1997", 
"26/11/1997", "27/11/1997", "28/11/1997", "35442", "35473", "35501", 
"35532", "35562", "35654", "35685", "35715", "35746", "35776", 
"15/12/1997", "16/12/1997", "17/12/1997", "18/12/1997", "19/12/1997", 
"22/12/1997", "23/12/1997", "24/12/1997", "25/12/1997", "26/12/1997", 
"29/12/1997", "30/12/1997", "31/12/1997", "35796", "35827", "35916", 
"35947", "35977", "36008", "36039", "36130", "13/1/1998", "14/1/1998", 
"15/1/1998", "16/1/1998", "19/1/1998", "20/1/1998", "21/1/1998", 
"22/1/1998", "23/1/1998", "26/1/1998", "27/1/1998", "28/1/1998", 
"29/1/1998", "30/1/1998", "35828", "35856", "35887", "35917", 
"35948", "36040", "36070", "36101", "36131", "13/2/1998", "16/2/1998", 
"17/2/1998", "18/2/1998", "19/2/1998", "20/2/1998", "23/2/1998", 
"24/2/1998", "25/2/1998", "26/2/1998", "27/2/1998", "35829", 
"35857", "35888", "35918", "35949", "36041", "36071", "36102", 
"36132", "13/3/1998", "16/3/1998", "17/3/1998", "18/3/1998", 
"19/3/1998", "20/3/1998", "23/3/1998", "24/3/1998", "25/3/1998", 
"26/3/1998", "27/3/1998", "30/3/1998", "31/3/1998", "35799", 
"35830", "35858", "35950", "35980", "36011", "36042", "36072", 
"13/4/1998", "14/4/1998", "15/4/1998", "16/4/1998", "17/4/1998", 
"20/4/1998", "21/4/1998", "22/4/1998", "23/4/1998", "24/4/1998", 
"27/4/1998", "28/4/1998", "29/4/1998", "30/4/1998", "35800", 
"35890", "35920", "35951", "35981", "36012", "36104", "36134", 
"13/5/1998", "14/5/1998", "15/5/1998", "18/5/1998", "19/5/1998", 
"20/5/1998", "21/5/1998", "22/5/1998", "25/5/1998", "26/5/1998", 
"27/5/1998", "28/5/1998", "29/5/1998", "35801", "35832", "35860", 
"35891", "35921", "36013", "36044", "36074", "36105", "36135", 
"15/6/1998", "16/6/1998", "17/6/1998", "18/6/1998", "19/6/1998", 
"22/6/1998", "23/6/1998", "24/6/1998", "25/6/1998", "26/6/1998", 
"29/6/1998", "30/6/1998", "35802", "35833", "35861", "35953", 
"35983", "36014", "36045", "36075", "13/7/1998", "14/7/1998", 
"15/7/1998", "16/7/1998", "17/7/1998", "20/7/1998", "21/7/1998", 
"22/7/1998", "23/7/1998", "24/7/1998", "27/7/1998", "28/7/1998", 
"29/7/1998", "30/7/1998", "31/7/1998", "35862", "35893", "35923", 
"35954", "35984", "36076", "36107", "36137", "13/8/1998", "14/8/1998", 
"17/8/1998", "18/8/1998", "19/8/1998", "20/8/1998", "21/8/1998", 
"24/8/1998", "25/8/1998", "26/8/1998", "27/8/1998", "28/8/1998", 
"31/8/1998", "35804", "35835", "35863", "35894", "35985", "36016", 
"36047", "36077", "36108", "14/9/1998", "15/9/1998", "16/9/1998", 
"17/9/1998", "18/9/1998", "21/9/1998", "22/9/1998", "23/9/1998", 
"24/9/1998", "25/9/1998", "28/9/1998", "29/9/1998", "30/9/1998", 
"35805", "35836", "35925", "35956", "35986", "36017", "36048", 
"36139", "13/10/1998", "14/10/1998", "15/10/1998", "16/10/1998", 
"19/10/1998", "20/10/1998", "21/10/1998", "22/10/1998", "23/10/1998", 
"26/10/1998", "27/10/1998", "28/10/1998", "29/10/1998", "30/10/1998", 
"35837", "35865", "35896", "35926", "35957", "36049", "36079", 
"36110", "36140", "13/11/1998", "16/11/1998", "17/11/1998", "18/11/1998", 
"19/11/1998", "20/11/1998", "23/11/1998", "24/11/1998", "25/11/1998", 
"26/11/1998", "27/11/1998", "30/11/1998", "35807", "35838", "35866", 
"35897", "35988", "36019", "36050", "36080", "36111", "14/12/1998", 
"15/12/1998", "16/12/1998", "17/12/1998", "18/12/1998", "21/12/1998", 
"22/12/1998", "23/12/1998", "24/12/1998", "25/12/1998", "28/12/1998", 
"29/12/1998", "30/12/1998", "31/12/1998", "36161", "36251", "36281", 
"36312", "36342", "36373", "36465", "36495", "13/1/1999", "14/1/1999", 
"15/1/1999", "18/1/1999", "19/1/1999", "20/1/1999", "21/1/1999", 
"22/1/1999", "25/1/1999", "26/1/1999", "27/1/1999", "28/1/1999", 
"29/1/1999", "36162", "36193", "36221", "36252", "36282", "36374", 
"36405", "36435", "36466", "36496", "15/2/1999", "16/2/1999", 
"17/2/1999", "18/2/1999", "19/2/1999", "22/2/1999", "23/2/1999", 
"24/2/1999", "25/2/1999", "26/2/1999", "36163", "36194", "36222", 
"36253", "36283", "36375", "36406", "36436", "36467", "36497", 
"15/3/1999", "16/3/1999", "17/3/1999", "18/3/1999", "19/3/1999", 
"22/3/1999", "23/3/1999", "24/3/1999", "25/3/1999", "26/3/1999", 
"29/3/1999", "30/3/1999", "31/3/1999", "36164", "36195", "36284", 
"36315", "36345", "36376", "36407", "36498", "13/4/1999", "14/4/1999", 
"15/4/1999", "16/4/1999", "19/4/1999", "20/4/1999", "21/4/1999", 
"22/4/1999", "23/4/1999", "26/4/1999", "27/4/1999", "28/4/1999", 
"29/4/1999", "30/4/1999", "36224", "36255", "36285", "36316", 
"36346", "36438", "36469", "36499", "13/5/1999", "14/5/1999", 
"17/5/1999", "18/5/1999", "19/5/1999", "20/5/1999", "21/5/1999", 
"24/5/1999", "25/5/1999", "26/5/1999", "27/5/1999", "28/5/1999", 
"31/5/1999", "36166", "36197", "36225", "36256", "36347", "36378", 
"36409", "36439", "36470", "14/6/1999", "15/6/1999", "16/6/1999", 
"17/6/1999", "18/6/1999", "21/6/1999", "22/6/1999", "23/6/1999", 
"24/6/1999", "25/6/1999", "28/6/1999", "29/6/1999", "30/6/1999", 
"36167", "36198", "36287", "36318", "36348", "36379", "36410", 
"36501", "13/7/1999", "14/7/1999", "15/7/1999", "16/7/1999", 
"19/7/1999", "20/7/1999", "21/7/1999", "22/7/1999", "23/7/1999", 
"26/7/1999", "27/7/1999", "28/7/1999", "29/7/1999", "30/7/1999", 
"36199", "36227", "36258", "36288", "36319", "36411", "36441", 
"36472", "36502", "13/8/1999", "16/8/1999", "17/8/1999", "18/8/1999", 
"19/8/1999", "20/8/1999", "23/8/1999", "24/8/1999", "25/8/1999", 
"26/8/1999", "27/8/1999", "30/8/1999", "31/8/1999", "36169", 
"36200", "36228", "36320", "36350", "36381", "36412", "36442", 
"13/9/1999", "14/9/1999", "15/9/1999", "16/9/1999", "17/9/1999", 
"20/9/1999", "21/9/1999", "22/9/1999", "23/9/1999", "24/9/1999", 
"27/9/1999", "28/9/1999", "29/9/1999", "30/9/1999", "36170", 
"36260", "36290", "36321", "36351", "36382", "36474", "36504", 
"13/10/1999", "14/10/1999", "15/10/1999", "18/10/1999", "19/10/1999", 
"20/10/1999", "21/10/1999", "22/10/1999", "25/10/1999", "26/10/1999", 
"27/10/1999", "28/10/1999", "29/10/1999", "36171", "36202", "36230", 
"36261", "36291", "36383", "36414", "36444", "36475", "36505", 
"15/11/1999", "16/11/1999", "17/11/1999", "18/11/1999", "19/11/1999", 
"22/11/1999", "23/11/1999", "24/11/1999", "25/11/1999", "26/11/1999", 
"29/11/1999", "30/11/1999", "36172", "36203", "36231", "36323", 
"36353", "36384", "36415", "36445", "13/12/1999", "14/12/1999", 
"15/12/1999", "16/12/1999", "17/12/1999", "20/12/1999", "21/12/1999", 
"22/12/1999", "23/12/1999", "24/12/1999", "27/12/1999", "28/12/1999", 
"29/12/1999", "30/12/1999", "31/12/1999", "36586", "36617", "36647", 
"36678", "36708", "36800", "36831", "36861", "13/1/2000", "14/1/2000", 
"17/1/2000", "18/1/2000", "19/1/2000", "20/1/2000", "21/1/2000", 
"24/1/2000", "25/1/2000", "26/1/2000", "27/1/2000", "28/1/2000", 
"31/1/2000", "36527", "36558", "36587", "36618", "36709", "36740", 
"36771", "36801", "36832", "14/2/2000", "15/2/2000", "16/2/2000", 
"17/2/2000", "18/2/2000", "21/2/2000", "22/2/2000", "23/2/2000", 
"24/2/2000", "25/2/2000", "28/2/2000", "29/2/2000", "36528", 
"36559", "36588", "36680", "36710", "36741", "36772", "36802", 
"13/3/2000", "14/3/2000", "15/3/2000", "16/3/2000", "17/3/2000", 
"20/3/2000", "21/3/2000", "22/3/2000", "23/3/2000", "24/3/2000", 
"27/3/2000", "28/3/2000", "29/3/2000", "30/3/2000", "31/3/2000", 
"36589", "36620", "36650", "36681", "36711", "36803", "36834", 
"36864", "13/4/2000", "14/4/2000", "17/4/2000", "18/4/2000", 
"19/4/2000", "20/4/2000", "21/4/2000", "24/4/2000", "25/4/2000", 
"26/4/2000", "27/4/2000", "28/4/2000", "36530", "36561", "36590", 
"36621", "36651", "36743", "36774", "36804", "36835", "36865", 
"15/5/2000", "16/5/2000", "17/5/2000", "18/5/2000", "19/5/2000", 
"22/5/2000", "23/5/2000", "24/5/2000", "25/5/2000", "26/5/2000", 
"29/5/2000", "30/5/2000", "31/5/2000", "36531", "36562", "36652", 
"36683", "36713", "36744", "36775", "36866", "13/6/2000", "14/6/2000", 
"15/6/2000", "16/6/2000", "19/6/2000", "20/6/2000", "21/6/2000", 
"22/6/2000", "23/6/2000", "26/6/2000", "27/6/2000", "28/6/2000", 
"29/6/2000", "30/6/2000", "36592", "36623", "36653", "36684", 
"36714", "36806", "36837", "36867", "13/7/2000", "14/7/2000", 
"17/7/2000", "18/7/2000", "19/7/2000", "20/7/2000", "21/7/2000", 
"24/7/2000", "25/7/2000", "26/7/2000", "27/7/2000", "28/7/2000", 
"31/7/2000", "36533", "36564", "36593", "36624", "36715", "36746", 
"36777", "36807", "36838", "14/8/2000", "15/8/2000", "16/8/2000", 
"17/8/2000", "18/8/2000", "21/8/2000", "22/8/2000", "23/8/2000", 
"24/8/2000", "25/8/2000", "28/8/2000", "29/8/2000", "30/8/2000", 
"31/8/2000", "36534", "36625", "36655", "36686", "36716", "36747", 
"36839", "36869", "13/9/2000", "14/9/2000", "15/9/2000", "18/9/2000", 
"19/9/2000", "20/9/2000", "21/9/2000", "22/9/2000", "25/9/2000", 
"26/9/2000", "27/9/2000", "28/9/2000", "29/9/2000", "36566", 
"36595", "36626", "36656", "36687", "36779", "36809", "36840", 
"36870", "13/10/2000", "16/10/2000", "17/10/2000", "18/10/2000", 
"19/10/2000", "20/10/2000", "23/10/2000", "24/10/2000", "25/10/2000", 
"26/10/2000", "27/10/2000", "30/10/2000", "31/10/2000", "36536", 
"36567", "36596", "36688", "36718", "36749", "36780", "36810", 
"13/11/2000", "14/11/2000", "15/11/2000", "16/11/2000", "17/11/2000", 
"20/11/2000", "21/11/2000", "22/11/2000", "23/11/2000", "24/11/2000", 
"27/11/2000", "28/11/2000", "29/11/2000", "30/11/2000", "36537", 
"36628", "36658", "36689", "36719", "36750", "36842", "36872", 
"13/12/2000", "14/12/2000", "15/12/2000", "18/12/2000", "19/12/2000", 
"20/12/2000", "21/12/2000", "22/12/2000", "25/12/2000", "26/12/2000", 
"27/12/2000", "28/12/2000", "29/12/2000", "36892", "36923", "36951", 
"36982", "37012", "37104", "37135", "37165", "37196", "37226", 
"15/1/2001", "16/1/2001", "17/1/2001", "18/1/2001", "19/1/2001", 
"22/1/2001", "23/1/2001", "24/1/2001", "25/1/2001", "26/1/2001", 
"29/1/2001", "30/1/2001", "31/1/2001", "36893", "36924", "37013", 
"37044", "37074", "37105", "37136", "37227", "13/2/2001", "14/2/2001", 
"15/2/2001", "16/2/2001", "19/2/2001", "20/2/2001", "21/2/2001", 
"22/2/2001", "23/2/2001", "26/2/2001", "27/2/2001", "28/2/2001", 
"36894", "36925", "37014", "37045", "37075", "37106", "37137", 
"37228", "13/3/2001", "14/3/2001", "15/3/2001", "16/3/2001", 
"19/3/2001", "20/3/2001", "21/3/2001", "22/3/2001", "23/3/2001", 
"26/3/2001", "27/3/2001", "28/3/2001", "29/3/2001", "30/3/2001", 
"36926", "36954", "36985", "37015", "37046", "37138", "37168", 
"37199", "37229", "13/4/2001", "16/4/2001", "17/4/2001", "18/4/2001", 
"19/4/2001", "20/4/2001", "23/4/2001", "24/4/2001", "25/4/2001", 
"26/4/2001", "27/4/2001", "30/4/2001", "36896", "36927", "36955", 
"36986", "37077", "37108", "37139", "37169", "37200", "14/5/2001", 
"15/5/2001", "16/5/2001", "17/5/2001", "18/5/2001", "21/5/2001", 
"22/5/2001", "23/5/2001", "24/5/2001", "25/5/2001", "28/5/2001", 
"29/5/2001", "30/5/2001", "31/5/2001", "36897", "36987", "37017", 
"37048", "37078", "37109", "37201", "37231", "13/6/2001", "14/6/2001", 
"15/6/2001", "18/6/2001", "19/6/2001", "20/6/2001", "21/6/2001")), .Names = "Date", row.names = c(NA, 
-6523L), class = c("tbl_df", "tbl", "data.frame"))

2 个答案:

答案 0 :(得分:2)

关于固定矢量的部分,假设日期按时间顺序排列并且相对较近(因此%d /%m和%m /%d不是两种可能的解决方案),您可以使用前一个值来定义下一个数字如下:

x <- c("19/9/1997","22/9/1997","23/9/1997","24/9/1997","25/9/1997","26/9/1997","29/9/1997","30/9/1997","10/01/1997","10/02/1997","10/03/1997","10/06/1997","10/07/1997","10/08/1997","10/09/1997","10/10/1997","13/10/1997","14/10/1997","15/10/1997","16/10/1997","17/10/1997","20/10/1997","21/10/1997","22/10/1997","23/10/1997","24/10/1997","27/10/1997","28/10/1997","29/10/1997","30/10/1997","31/10/1997","11/03/1997","11/04/1997","11/05/1997","11/06/1997","11/07/1997","11/10/1997","11/11/1997","11/12/1997","13/11/1997","14/11/1997","17/11/1997","18/11/1997","19/11/1997","20/11/1997","21/11/1997","24/11/1997","25/11/1997","26/11/1997","27/11/1997","28/11/1997","12/01/1997","12/02/1997","12/03/1997","12/04/1997","12/05/1997","12/08/1997","12/09/1997","12/10/1997","12/11/1997","12/12/1997","15/12/1997","16/12/1997","17/12/1997","18/12/1997","19/12/1997","22/12/1997","23/12/1997","24/12/1997","25/12/1997","26/12/1997","29/12/1997","30/12/1997","31/12/1997","1/01/1998","1/02/1998","1/05/1998","1/06/1998","1/07/1998","1/08/1998","1/09/1998","1/12/1998","13/1/1998","14/1/1998","15/1/1998","16/1/1998","19/1/1998","20/1/1998","21/1/1998","22/1/1998","23/1/1998","26/1/1998","27/1/1998","28/1/1998","29/1/1998","30/1/1998")

ans <- Reduce(function(prev, curr) {
    f1 <- as.Date(curr, "%d/%m/%Y")
    f2 <- as.Date(curr, "%m/%d/%Y")
    if (is.na(f1)) return(f2)
    if (is.na(f2)) return(f1)
    if (prev < f1 && prev < f2) return(min(f1, f2))
    if (prev < f1) return(f1)
    if (prev < f2) return(f2)
}, x[-1], init=as.Date(x[1], "%d/%m/%Y"), accumulate=TRUE)

as.Date(ans, origin="1970-01-01")

答案 1 :(得分:1)

希望这有帮助!

library(lubridate)
library(dplyr)

df %>% 
  mutate(converted = ifelse(nchar(V1) >= 8, 
                            as.character(parse_date_time(V1, orders = "%d/%m/%Y")), 
                            as.character(parse_date_time(as.Date(as.numeric(V1), origin="1899-12-30"), orders = "%Y-%d-%m"))))

输出为:

          V1  converted
1      33274 1991-05-02
2      33302 1991-05-03
3      33394 1991-05-06
4      33424 1991-05-07
5      33455 1991-05-08
6      33486 1991-05-09
7      33516 1991-05-10
8  13/5/1991 1991-05-13
9  14/5/1991 1991-05-14
10 15/5/1991 1991-05-15
...

示例数据:

df <- structure(list(V1 = c("33274", "33302", "33394", "33424", "33455", 
"33486", "33516", "13/5/1991", "14/5/1991", "15/5/1991", "16/5/1991", 
"17/5/1991", "20/5/1991", "21/5/1991", "22/5/1991", "23/5/1991", 
"24/5/1991", "27/5/1991", "28/5/1991", "29/5/1991", "30/5/1991", 
"31/5/1991", "33303", "33334", "33364", "33395", "33425", "33517", 
"33548", "33578", "13/6/1991", "14/6/1991", "17/6/1991", "18/6/1991", 
"19/6/1991", "20/6/1991", "21/6/1991")), .Names = "V1", class = "data.frame", row.names = c(NA, 
-37L))