Enjoy the power of lubridate :
library(lubridate) mdy(ord_dates) [1] "2016-09-01" "2016-09-02" "2016-09-03" "2016-09-04"
Internally, lubridate does not have special conversion specifications that allow this. Rather, lubridate first uses (by smart fortune telling) the format "%B %dst, %Y" . This gets the first element of ord_dates .
He then checks the NA and repeats his clever guessing of the rest of the elements, dropping to "%B %dnd, %Y" to get the second element. This continues until NA left on the left (which happens in this case after 4 iterations), or until his intellectual guessing can not display the candidate of the likely format.
You can imagine that this makes lubridate slower, and it does - about half the speed of just using the intelligent regular expression suggested by @alistaire above:
set.seed(109123) ord_dates <- sample( c("September 1st, 2016", "September 2nd, 2016", "September 3rd, 2016", "September 4th, 2016"), 1e6, TRUE ) library(microbenchmark) microbenchmark(times = 10L, lubridate = mdy(ord_dates), base = as.Date(sub("\\D+,", "", ord_dates), format = "%B %e %Y"))
An obvious advantage in lubridate is its brevity and flexibility.
source share