Does anyone know a way to parse dates through guessing by using a single format - and not by using lubridate
or any other package?
This is not a duplicate, as I cannot find any answer that would deal with that outside the packages.
For instance, before switching to guess_formats
I've sometimes used functions like below to identify either ymd
or dmy
formats and turn them to ymd
.
to_ymd <- function(x) {
x <- gsub("[^0-9]", "/", x)
is_dmy <- any(grepl("\\/", substr(x, 6, 6)))
if (is_dmy == TRUE) {
x <- as.Date(as.character(x), format = "%d/%m/%Y")
} else {
x <- as.Date(as.character(x), format = "%Y/%m/%d")
}
return(x)
}
However, this is rather slow, and it certainly isn't robust as it doesn't take into account other possibilities.
Does anyone know a faster base R
way to guess and unify formats (most importantly ymd
, dmy
, but also mdy
, formats without delimiters, etc.)?