[R] Problem with seq.dates in chron

Gabor Grothendieck ggrothendieck at myway.com
Wed Sep 1 03:24:42 CEST 2004

Waichler, Scott R <Scott.Waichler <at> pnl.gov> writes:

> I get faulty output from seq.dates() if I specify a length that is too
> long.
> For example, I ask for 129 months in the following call to the function,
> but it returns
> 131:
> > R.version.string
> [1] "R version 1.9.1, 2004-06-21"
> > startdatetime <- chron(dates="01/01/1995", times="00:00:00")
> > beg.month.datetimes <- seq.dates(from=startdatetime, by="months",
> length=129)

If you look at the seq.dates R source, it sets `to' like this:

   to <- from + (length. - 1) * c(1, 7, 31, 366)[i]

where in your case where i is 3 for months.   It then
picks out those days between from and to having the same day 
of the month as the from date (or uses a more complex algorithm if the day 
of the month exceeds 28).  The multiplication by 31 gives an overestimate 
which is OK for a small lengths but overflows to give too many months if
length is too large.  Since we know the error will always be on the
high side, until it is fixed as a workaround you could just subset it:

   seq.dates(from=startdatetime, by="months", length=129)[1:129]

(Perhaps you could forward this to the maintainer of chron.)

More information about the R-help mailing list