I have a character vector of dates in format DDMMYYY (millenium character ommitted), that I have to convert in a vector of dates.
dates <- c("0410988", "2305009", "1111964", "0204015", "1803015", "0709015","0401015", "2012015", "3004158", "1205015")
These are the expected output dates:
2009-05-23, 1964-11-11, 2015-04-02, 2015-03-18, 015-09-07, 2015-01-04, 2015-12-20, 2158-04-30, 2015-05-12
I tried removing the first Y character and using the regular as.Date()
with format= %d%m%y
:
dates <- c("0410988", "2305009", "1111964", "0204015", "1803015", "0709015","0401015", "2012015", "3004158", "1205015")%\>%
sapply(dates, function(x) paste0(substr(x, 1, 4), substr(x, 6, nchar(x)))) %\>%
as.Date(., format = "%d%m%y")
But this clearly doesn't work: 1111964 gets converted to 2064-11-11 instead of 1964-11-11, and 3004158 gets converted to 2058-04-30 instead of 2158-04-30 (this date is ambiguous as it is).
I also tried using substring()
to extract the characters representing the day, the month and the year separately, and then plugging them into make_date()
. However, this doesn't work either with only 3 numbers per year (here is just the example how 1111964 would work):
make_date("964", "11", "11")
[1] "964-11-11"
I can't just add 1000 to the year, since it won't work for years after 2000, so I assume there has to be a better way for such conversion.