0

How to convert the data such as "20nov2016" in the date?

the variable was saved as factor; I did it:

df$airdates = as.character(df$airdates)

library(lubridate)
as_date(df$airdates)

tmp2 = gsub("\n", "", df$airdates[1]) %>% trimws() #replaced and removed 
extra marks
tmp2 = gsub("\\.", "", tmp2) #replaced and removed extra marks
tmp2 = gsub(" ", "", tmp2) #replaced and removed extra marks
tmp2 = tolower(tmp2) #data look like "20nov2016"
as.Date(tmp2, format = "%d%h%Y") #in this step it creates NA

R refuses to take it as a date and create NA`s. What am I doing wrong?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

3

You could use as.Date

as.Date("20nov2016", "%d%b%Y")
#[1] "2016-11-20"

Or lubridate

lubridate::dmy("20nov2016")

Or anytime::anydate

anytime::anydate("20nov2016")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213