Dear Posit
I need your help to convert odd date formats to a standard date and datetime formats.
desired_date and desired_date2 are my target columns.
Repex is pasted here.
I hope you will help me in this.
Thank you.
Here is one approach. Note I converted your tibble to a data.table just because the syntax is simpler for me.
Then I converted the data back to a tibble---probably not needed but you started with a tibble.
Unless you have specific rules regarding month and day of month priority and leading zeros in service_date, I'm afraid that format is ambiguous.
For example, what would be the valid encoding for the 1st of January? 112025? Or 1012025 ? Or is the latter used for 10th of January instead?
Or what would be the expected date for 1112025? 1st of November? 11th of January?
You'd probably want to trace back to the origin of service_dateand fix it or ask it to be fixed there, even if it's not part of a workflow or process that's directly under your own control.
Hi, thanks for your comments.
Yes, i have no control over the source data.
Service date format = dropping a leading zero, so 1012025 is Jan 1, 2025
If you could help convert service date to desired formats then I would really appreciate.
Thank you so much.
@jrkrideau
I need to convert the service date to the desired columns as shown.
service date format = dropping a leading zero in a sense that 1012025 is Jan 1, 2025
Thank you.
@ prubin
Ah, so we convert `service_date" to a character variable, add a 0 to anything that needs it and convert to date? It's too late here for me to try it but it looks feasible.
@jrkrideau I think this reproduces the original (incorrect) results but not the corrected version. In particular, the target for the first row is now "2/01/2025" but I get "20/01/2025" running your code.
Problem is that such formats can be either engineered or happen by accident, in latter case leading zeros might as well be dropped from all other fields as well, (i.e. from moths)
For quick data check you could run all(nchar(mydata$service_date) > 6), if it's FALSE I'm afraid those expectations are not valid.
You can pad strings to desired length with stringr::str_pad(), coercion from integers happens atomagically; and you can find a list of date/time format specifiers for format() from ?strptime