0

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.)?

arg0naut91
  • 14,574
  • 2
  • 17
  • 38
  • 2
    Not sure how you can tell the difference between some of the date formats. Example, what format is this:04/09/2018? – clinomaniac Nov 05 '18 at 22:31
  • 1
    Another answer that may help you here: https://stackoverflow.com/a/52319606/3358272 – r2evans Nov 05 '18 at 22:35
  • 1
    What is your point about "yes to guessing over different format" and "no to other packages" ? My [anytime](https://cloud.r-project.org/web/packages/anytime/index.html) package implements _just this_ over looping over [this list of known formats](https://github.com/eddelbuettel/anytime/blob/master/src/anytime.cpp#L43-L106). It is being downloaded a few thousand times each month so is moderately popular. And @clinomaniac here I chose to prefer (and coument) the preference of ISO formats over ambiguous formats so we won't guess ddmmyyyy. – Dirk Eddelbuettel Nov 05 '18 at 22:36
  • @clinomaniac, I deal with relatively large date variation so > 12 in any part would almost always be an indicator; however, the possibility of encountering mdy in my case is rather rare and thus not an issue. – arg0naut91 Nov 05 '18 at 22:44
  • @Dirk Eddelbuettel, from what I hear your package is quite useful, but while I have nothing against various packages (especially if Rcpp is behind), let's just say they don't fit into my current daily framework. – arg0naut91 Nov 05 '18 at 22:44
  • 1
    I am still curious why, and you still haven't explained it besides repeating the outcome but not the reasoning. I wrote `anytime` for my use and it works for me; putting it out here made it better (new useful features) and removed bugs (more eyes, more testers). But whatever works for you. – Dirk Eddelbuettel Nov 05 '18 at 22:47
  • 2
    @arg0naut I don't know what you mean by your "daily framework" but you could literally rebuild `anytime` from scratch if it's an issue of not being able to install packages from CRAN/the internet. Otherwise, you can essentially re-write a portion of what `anytime` does in R, but it seems pretty unnecessary. – Mako212 Nov 05 '18 at 23:30

0 Answers0