Convert data type

Very often I work with excel files which are in very messy formats, such as ones including columns of dates with inconsistent data types. When I import this kind of messy data to R using the read_excel() function, the dates are automatically loaded in character format. If I apply the as.Date() function, I will get NA in the full column.

The solution which works for me is to: 1. Convert the date from character to factor 2. Converting factor into POSIXt 3. Converting POSIXt to date

knitr::opts_chunk$set(echo = TRUE)
a <- "24/06/2020"
str(a)
##  chr "24/06/2020"
b<-as.factor(a)
str(b)
##  Factor w/ 1 level "24/06/2020": 1
c<-as.Date(a,format="%Y-%m-%d")
str(c)
##  Date[1:1], format: NA
d <- strptime(b,format="%d/%m/%Y") #defining what is the original format of your date
str(d)
##  POSIXlt[1:1], format: "2020-06-24"
e<-as.Date(d,format="%Y-%m-%d")
str(e)
##  Date[1:1], format: "2020-06-24"