0

I'm having a small problem with the parsedate package in R, I have a large dataset with A LOT of messy dates that it is handling extremely well. However, with ambiguous dates, i.e. those where the date and month are both < 12, it will default to parse them as mm/dd/yyyy. I would instead prefer it to assume dd/mm/yyyy instead in those cases... Has anyone had any joy with this?

For clarity I have also tried lubridate and as.Date but with the amount of different messy date formats in my data they were just not viable options.

Simplified code I have run:

parse_date("09-06-2021")

Gives "2021-09-06 UTC" as an output whereas I want "2021-06-09 UTC"

Thanks in advance!

  • 1
    Try `lubridate::dmy()`. – Rui Barradas Apr 28 '23 at 11:28
  • 1
    It doesn't appear that the `parse_date` allows control over the individual formats used to attempt parsing. If you know a priori what the format is, I recommend using either `as.Date` or `as.POSIXt` (depending on your target class) or using one of the `lubridge::` functions such as `dmy` as RuiBarradas suggested. If you don't know or the formats are mixed, then you may benefit from a home-grown solution: https://stackoverflow.com/a/52319606/3358272 (dates), https://stackoverflow.com/a/70304571/3358272 (POSIXct) – r2evans Apr 28 '23 at 11:40
  • Thanks! I was afraid it didn't allow control over the specific formats... Unfortunately I dont know the specific formats, or at least, I wont as I add new data to this dataset. It may just be a case of just taking new observations and bespoke coding the parsing in each case... – James O'Malley Apr 28 '23 at 11:50
  • 1
    Also, both [`as.POSIXct`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/as.POSIXlt.html) (since you mentioned `UTC` in your question) and [`as.Date`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/as.Date.html) (since it looks like a date) support `tryFormats`. – r2evans Apr 28 '23 at 12:45

0 Answers0