I have a data frame column of dates (in character format) with a mixture of dates:
("Apr11", "2005-01-01", "Apr13", "2009-12-01")
I tried to use the lubridate parse_date_time() function to parse these dates as follows:
parse_date_time(x = variable, orders = c('y-m-d', 'by'), locale='en_US.UTF-8')
While parse_date_time() is able to parse dates with the format 'y-m-d', it fails to parse dates with the format 'by'.
I then tried to experiment with a toy example but with no success:
z <- "Apr11"
parse_date_time(z,"by")
I keep getting the same error:
[1] NA
Warning message:
All formats failed to parse. No formats found.
I read the documentation and tried a number of different things with no luck:
- I set locale to the default_locale() function.
- I tried B instead of b
- I tried setting setting the exact option to True as given by the note in the documentation
- I tried the parse_date_time2() function which worked on the toy example, but didn't work on the original dataframe with the mixture of dates (This threw a warning message - Multiple orders supplied. Only first order is used.)
I realize that b is locale sensitive but I don't see the issue here - Here's the output from default_locale()
<locale>
Numbers: 123,456.78
Formats: %AD / %AT
Timezone: UTC
Encoding: UTF-8
<date_names>
Days: Sunday (Sun), Monday (Mon), Tuesday (Tue), Wednesday (Wed), Thursday (Thu), Friday (Fri), Saturday (Sat)
Months: January (Jan), February (Feb), March (Mar), April (Apr), May (May), June (Jun), July (Jul), August (Aug), September (Sep),
October (Oct), November (Nov), December (Dec)
AM/PM: AM/PM
And here's the output from Sys.getlocale()
[1] "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C"
I've also looked into various posts on stackoverflow but nothing helped:
- Changing date format in R
- How to change multiple Date formats in same column
- convert variable with mixed date formats to one format in r
- Why R package lubridate can't parse vector with multiple formats?
Thanks in advance!