Question

So i have a dataframe which has date columns and all the columns can have different formats

> DateCol1      DateCol2     DateCol3       DateCol4      DateCol5
> 24-08-2011   2011-12-24     08/1900/24    12/13/2011    Jan 31 1895

I know that i can use strptime/as.Date/as.POSIXct for each of them individually but since the dataframe will be generated on run time , i won't know the contents beforehand

So how do I convert all of them to the default R format in a generic function/statement ?

Was it helpful?

Solution

Try using guess_formats from the lubridate package:

library(lubridate)
fmts <- lapply(dat, guess_formats, c("m d y", "d-M-y", "Y-m-d",  "m Y d"))
fmts <- lapply(fmts, "[[", 1)
as.data.frame(mapply(parse_date_time, x=dat, orders=fmts))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top