2
Date  
01/01/2013 #mm/dd/yyyy  
12122015 #mmddyyyy  
2014-03-21 #yyyy-mm-dd  
95.10.12 #yy.mm.dd  

I have a column "Date" with different formats. How can I clean this and convert them into a single date format?
Additional info:class(Date) is factor.

akrun
  • 674,427
  • 24
  • 381
  • 486

1 Answers1

3

The easiest way to do this is to use the lubridate package.

Date=c( 
   "01/01/2013" #mm/dd/yyyy  
  ,"12122015" #mmddyyyy  
  ,"2014-03-21" #yyyy-mm-dd  
  ,"95.10.12" #yy.mm.dd  
)

library(lubridate)

# list of functions in lubridate that 
# translate text to POSIXct: you only need to know the
# order of the data (e.g. mdy = month-day-year).
funs <- c("mdy","dym","ymd","dmy","myd","ydm")

# vector to store results
dates <- as.POSIXct(rep(NA,length(Date)))

# we try everything lubridate has. There will be some warnings
# e.g. because mdy cannot translate everything. You can ignore this.
for ( f in funs ){
  dates[is.na(dates)] <- do.call(f,list(Date[is.na(dates)]))  
}
dates

> dates
[1] "2013-01-01 01:00:00 CET" "2015-12-12 01:00:00 CET" "2013-01-01 01:00:00 CET" "2015-12-12 01:00:00 CET"