Reformat Date from DD-MM-YYYY -> YYYY-MM-DD

I currently have a data source where the format in the column date is (DD-MM-YYYY) for my analysis the script is not processing the date correctly because it needs to be in the format of (YYYY-MM-DD). When I look at my current date here is the output. These dates are currently being used in a plot

> str(provinces$date)
 Factor w/ 73 levels "01-03-2020","01-04-2020",..: 72 72 72 19 19 19 35 35 35 46 ...

I took a look at some other solutions and saw that one method to do this was with.

dateNew <- as.Date(date, format = "%d %b %y")

However, when I process this, I am presented with the error

 Date[1:912], format: NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA ...

Any tips on how to reformat the date column?

Your format string is not quite right.

DATE <- c("01-03-2020", "01-04-2020")
dateNew <- as.Date(DATE, format = "%d-%m-%Y")
dateNew
#> [1] "2020-03-01" "2020-04-01"

Created on 2020-05-06 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.