rdatechron

Why is the chron package treating 2 digit years oddly starting from 2030?


I recently noticed an odd behavior in R I can't explain. I had this in some old code:

The following should produce the list of months from 01/1980 -> 01/2029 and it works as expected:

length(chron::seq.dates("01/31/80", "01/03/29", by="months"))
[1] 588

This is where things become strange. The following is the same as above but should produce dates until the year 2030:

length(chron::seq.dates("01/31/80", "01/03/30", by="months"))
Error during wrapup: "from" must be a date before "to"

So what is happening here?


Solution

  • When expanding a two digit year to a 4 digit year the chron cutoff is 30 by default. That is, if the two digit year is less than 30 it is assumed to be 20yy and otherwise 19yy. This is controlled by the chron.year.expand option which is by default set to the chron year.expand function which in turn has a default cutoff of 30 but this can be changed as follows:

    library(chron)
    
    # change cutoff to 50
    options(chron.year.expand = 
         function (y, cut.off = 50, century = c(1900, 2000), ...) {
            chron:::year.expand(y, cut.off = cut.off, century = century, ...)
         }
    )
    
    length(seq.dates("01/31/80", "01/03/30", by="months"))
    ## [1] 600
    

    Each of these also work and do not require that chron.year.expand be set:

    length(seq(as.chron("1980-01-31"), as.chron("2030-01-03"), by="months"))
    
    length(seq.dates("01/31/80", as.chron("2030-01-03"), by="months"))
    
    length(seq.dates("01/31/80", chron(julian(1, 3, 2030)), by="months"))
    
    length(seq.dates("01/31/80", julian(1, 3, 2030), by="months"))
    
    length(as.chron(seq(as.Date("1980-01-31"), as.Date("2030-01-03"), by = "month")))
    
    length(seq.dates("01/31/80", length = 600, by="months"))