Search code examples
rexceldatey2k

r - Overcoming Y2K with as.Date


I have a list of dates that I know are in the past but are in the form 28/MAY/13. The closest way to make a date class out of them is the basic

dates <- as.Date(dates, format="%d/%b/%y")

which works well for all dates except for dates earlier than 1968 as the ?as.Date page notes:

%y Year without century (00–99). On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 – that is the behaviour specified by the 2004 and 2008 POSIX standards, but they do also say ‘it is expected that in a future version the default century inferred from a 2-digit year will change’.

Excel does a bit better for this, assuming (correctly in this case) that everything with a year above 30 is 1930 etc. But I would prefer to go back to 1914 if possible. How can I demand R interpret all dates as in the past?


Solution

  • Something like this:

    Sys.setlocale("LC_TIME", "English")
    
    dates <- as.Date(c("28/MAY/13","28/MAY/14"), format="%d/%b/%y")
    #[1] "2013-05-28" "2014-05-28"
    
    sub100 <- function(x) {
      x <- as.POSIXlt(x)
      x$year <- x$year-100
      as.Date(x)
    }
    
    
    dates[dates > as.Date("2013-12-31")] <- sub100(dates[dates > as.Date("2013-12-31")])
    #[1] "2013-05-28" "1914-05-28"