I need to convert a string of dates that is in multiple formats to valid dates.
e.g.
dates <- c(\"01-01-2017\",\"02-01-2017\",\"12-01-2016\",\"20
This is pretty much I wrote the anytime package for:
R> dates <- c("01-01-2017","02-01-2017","12-01-2016","20160901","20161001",
+ "20161101")
R> library(anytime)
R> anydate(dates)
[1] "2017-01-01" "2017-02-01" "2016-12-01" "2016-09-01"
[5] "2016-10-01" "2016-11-01"
R>
Parse any sane input reliably and without explicit format or origin or other line noise.
That being said, not starting ISO style with the year is asking for potential trouble, so 02-03-2017
could be February 3 or March 2. I am following the North American convention I too consider somewhat broken -- but is so darn prevalent. Do yourself a favour and try to limit inputs to ISO dates, at least ISO order YYYYMMDD.
I have tried library(anytime), however for big data did not work. Then, I found useful this sequence:
df$Date2 <- format(as.Date(df$Date, format="%m/%d/%Y"), "%d/%m/%y")
df$Date2 <- as.Date(df$Date2,"%d/%m/%y")
It worked for me to "8/10/2005" as well as "08/13/05" in the same column.