as.Date with dates in format m/d/y in R

Use capital Y in as.Date call instead. This should do the trick:

> as.Date("3/15/2012", "%m/%d/%Y")
[1] "2012-03-15"

From the help file’s examples you can realize when year is full specified you should use %Y otherwise %y for example:

> dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92")
> as.Date(dates, "%m/%d/%y")
[1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28" "1992-02-01"

You can see that in your example the Year format is 2012 then you should use %Y, and in the other example (taken from the as.Date help file) Year format is 92 then using %y is the correct way to go. See as.Date for further details.

Leave a Comment