Converting Date formats in R [closed]

Good practice is to store date in R as YYYY-MM-DD, and your strings already seem to be at the good format, but :

The format you’re passing to as.Date must describe what the strings contain, not what you’re expecting as an output.

"%d/%b/%Y" stands for “day as a number (0-31) slash abbreviated month slash 4-digit year”, and your strings format are “4-digit year – month as a number – day as a number”.

If you want to format the date, you need to call format :

> date <- "2016-01-01"
> date <- as.Date(date, format = "%Y-%m-%d")
> date
[1] "2016-01-01"
> format(date, "%d/%b/%Y")
[1] "01/jan/2016"

Leave a Comment