I am having trouble understanding the use of the guess_formats function in lubridate. I have a date vector in some unknown set / order of formats. I would like to convert them to a Date object (or at least convert as many as possible). The following code is what I tried:
library(lubridate) sampleDates <- c("4/6/2004","4/6/2004","4/6/2004","4/7/2004", "4/6/2004","4/7/2004","2014-06-28","2014-06-30","2014-07-12", "2014-07-29","2014-07-29","2014-08-12") formats <- guess_formats(sampleDates, c("Ymd", "mdY")) dates <- as.Date(sampleDates, format=formats)
This gives everything NA.
This is obviously just a short example. In the real case, I do not know where the various formats are scattered, and I would not be 100% sure that there is only% m /% d /% Y and% Y-% m-% d. Can someone let me know either A. How would I use gt_formats in this example or B. is there something more suitable for use in lubridate / base R, hopefully without a lot of regular expressions. Thanks!
Edit: I also tried parse_date_time. What I do not understand, the following works for this example:
parse_date_time(sampleDates, orders = c("Ymd", "mdY"), locale = "eng")
But this is not so:
parse_date_time(sampleDates, orders = c("mdY", "Ydm"), locale = "eng")
In my actual dataset, I will not know the formatting order, which seems to be important for this function.
Double editing: Dur, OK, I see that Ymd has parse_date_time in the first example and Ydm in the second ... continue.