Pergunta

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?

Foi útil?

Solução

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"

Outras dicas

A small refinement of Roland's answer. Rather than having a whole new sub100 function, just use lubridate's year function.

library(lubridate)
dates <- as.Date(c("28/MAY/13","28/MAY/14"), format="%d/%b/%y")
after_cut_off <- dates > as.Date("2013-12-31")
dates[after_cut_off] <- dates[after_cut_off] - years(100) #this is the new bit

Maybe I'm dense, but why not use %Y (capital)? Following ?strptime, this should give a year with century.

I got one solution which works perfectly for me.

Y2K Excel Problem: In all versions of excel 2 words date (month-year) or (mon-01) considered as 1901 rather than 2001, when converted with "=YEAR()" function. Normally it happened when we download old database having, 2 words date format into excel.

Y2K Excel Solution, (not VBA): All (columns/rows) with such date format copy it as text, then use small formula to convert it into proper year format (it works well from 2001 till 2099), but can be modified after 3000 as well. I am sure till then Microsoft come up with better solution.

Y2K Excel formula: C1="20"&RIGHT(B1,2). A1: original date B1: copied date into text form C1: formula for converting it into correct year.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top