Skip to content
Prev 164345 / 398506 Next

Confusion with Converting Factors to Dates using as.date

on 12/10/2008 02:41 PM Josip Dasovic wrote:
This is the consequence of using a two digit year rather than a four
digit year, which BTW, was one of the Y2K issues raised a decade ago...

As per ?strptime:

%y
    Year without century (00?99). If you use this on input, which
century you get is system-specific. So don't! Often values up to 68 (or
69) are prefixed by 20 and 69 (or 70) to 99 by 19.



If you know that all of your dates are going to be before 2000, you can
do the following, by using a regex to convert the two digit year to a
four digit year and then use as.Date() with '%Y':

st_date <- "01/15/63"
[1] "01/15/1963"
[1] "1963-01-15"


The better option is to ensure that the source of your data outputs or
exports dates with a four digit year, before importing into R.

See ?sub and ?regex

HTH,

Marc Schwartz