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)> beg.month.datetimes[1] 01/01/95 02/01/95 03/01/95 04/01/95 05/01/95 06/01/95 07/01/95 08/01/95 [9] 09/01/95 10/01/95 11/01/95 12/01/95 01/01/96 02/01/96 03/01/96 04/01/96 [17] 05/01/96 06/01/96 07/01/96 08/01/96 09/01/96 10/01/96 11/01/96 12/01/96 [25] 01/01/97 02/01/97 03/01/97 04/01/97 05/01/97 06/01/97 07/01/97 08/01/97 [33] 09/01/97 10/01/97 11/01/97 12/01/97 01/01/98 02/01/98 03/01/98 04/01/98 [41] 05/01/98 06/01/98 07/01/98 08/01/98 09/01/98 10/01/98 11/01/98 12/01/98 [49] 01/01/99 02/01/99 03/01/99 04/01/99 05/01/99 06/01/99 07/01/99 08/01/99 [57] 09/01/99 10/01/99 11/01/99 12/01/99 01/01/00 02/01/00 03/01/00 04/01/00 [65] 05/01/00 06/01/00 07/01/00 08/01/00 09/01/00 10/01/00 11/01/00 12/01/00 [73] 01/01/01 02/01/01 03/01/01 04/01/01 05/01/01 06/01/01 07/01/01 08/01/01 [81] 09/01/01 10/01/01 11/01/01 12/01/01 01/01/02 02/01/02 03/01/02 04/01/02 [89] 05/01/02 06/01/02 07/01/02 08/01/02 09/01/02 10/01/02 11/01/02 12/01/02 [97] 01/01/03 02/01/03 03/01/03 04/01/03 05/01/03 06/01/03 07/01/03 08/01/03 [105] 09/01/03 10/01/03 11/01/03 12/01/03 01/01/04 02/01/04 03/01/04 04/01/04 [113] 05/01/04 06/01/04 07/01/04 08/01/04 09/01/04 10/01/04 11/01/04 12/01/04 [121] 01/01/05 02/01/05 03/01/05 04/01/05 05/01/05 06/01/05 07/01/05 08/01/05 [129] 09/01/05 10/01/05 11/01/05>Is this a bug, and can it be fixed? Scott Waichler Pacific Northwest National Laboratory scott.waichler<at>pnl.gov
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.)
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)> beg.month.datetimes[1] 01/01/95 02/01/95 03/01/95 04/01/95 05/01/95 06/01/95 07/01/95 08/01/95 [9] 09/01/95 10/01/95 11/01/95 12/01/95 01/01/96 02/01/96 03/01/96 04/01/96 [17] 05/01/96 06/01/96 07/01/96 08/01/96 09/01/96 10/01/96 11/01/96 12/01/96 [25] 01/01/97 02/01/97 03/01/97 04/01/97 05/01/97 06/01/97 07/01/97 08/01/97 [33] 09/01/97 10/01/97 11/01/97 12/01/97 01/01/98 02/01/98 03/01/98 04/01/98 [41] 05/01/98 06/01/98 07/01/98 08/01/98 09/01/98 10/01/98 11/01/98 12/01/98 [49] 01/01/99 02/01/99 03/01/99 04/01/99 05/01/99 06/01/99 07/01/99 08/01/99 [57] 09/01/99 10/01/99 11/01/99 12/01/99 01/01/00 02/01/00 03/01/00 04/01/00 [65] 05/01/00 06/01/00 07/01/00 08/01/00 09/01/00 10/01/00 11/01/00 12/01/00 [73] 01/01/01 02/01/01 03/01/01 04/01/01 05/01/01 06/01/01 07/01/01 08/01/01 [81] 09/01/01 10/01/01 11/01/01 12/01/01 01/01/02 02/01/02 03/01/02 04/01/02 [89] 05/01/02 06/01/02 07/01/02 08/01/02 09/01/02 10/01/02 11/01/02 12/01/02 [97] 01/01/03 02/01/03 03/01/03 04/01/03 05/01/03 06/01/03 07/01/03 08/01/03 [105] 09/01/03 10/01/03 11/01/03 12/01/03 01/01/04 02/01/04 03/01/04 04/01/04 [113] 05/01/04 06/01/04 07/01/04 08/01/04 09/01/04 10/01/04 11/01/04 12/01/04 [121] 01/01/05 02/01/05 03/01/05 04/01/05 05/01/05 06/01/05 07/01/05 08/01/05 [129] 09/01/05 10/01/05 11/01/05>Is this a bug, and can it be fixed? Scott Waichler Pacific Northwest National Laboratory scott.waichler<at>pnl.gov
I think we just discussed this 2 days ago: https://www.stat.math.ethz.ch/pipermail/r-help/2004-September/055323.html 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) : > beg.month.datetimes : [1] 01/01/95 02/01/95 03/01/95 04/01/95 05/01/95 06/01/95 07/01/95 : 08/01/95 : [9] 09/01/95 10/01/95 11/01/95 12/01/95 01/01/96 02/01/96 03/01/96 : 04/01/96 : [17] 05/01/96 06/01/96 07/01/96 08/01/96 09/01/96 10/01/96 11/01/96 : 12/01/96 : [25] 01/01/97 02/01/97 03/01/97 04/01/97 05/01/97 06/01/97 07/01/97 : 08/01/97 : [33] 09/01/97 10/01/97 11/01/97 12/01/97 01/01/98 02/01/98 03/01/98 : 04/01/98 : [41] 05/01/98 06/01/98 07/01/98 08/01/98 09/01/98 10/01/98 11/01/98 : 12/01/98 : [49] 01/01/99 02/01/99 03/01/99 04/01/99 05/01/99 06/01/99 07/01/99 : 08/01/99 : [57] 09/01/99 10/01/99 11/01/99 12/01/99 01/01/00 02/01/00 03/01/00 : 04/01/00 : [65] 05/01/00 06/01/00 07/01/00 08/01/00 09/01/00 10/01/00 11/01/00 : 12/01/00 : [73] 01/01/01 02/01/01 03/01/01 04/01/01 05/01/01 06/01/01 07/01/01 : 08/01/01 : [81] 09/01/01 10/01/01 11/01/01 12/01/01 01/01/02 02/01/02 03/01/02 : 04/01/02 : [89] 05/01/02 06/01/02 07/01/02 08/01/02 09/01/02 10/01/02 11/01/02 : 12/01/02 : [97] 01/01/03 02/01/03 03/01/03 04/01/03 05/01/03 06/01/03 07/01/03 : 08/01/03 : [105] 09/01/03 10/01/03 11/01/03 12/01/03 01/01/04 02/01/04 03/01/04 : 04/01/04 : [113] 05/01/04 06/01/04 07/01/04 08/01/04 09/01/04 10/01/04 11/01/04 : 12/01/04 : [121] 01/01/05 02/01/05 03/01/05 04/01/05 05/01/05 06/01/05 07/01/05 : 08/01/05 : [129] 09/01/05 10/01/05 11/01/05 : > : : Is this a bug, and can it be fixed?
The following naive calculation in seq.dates explains this: to <- from + (length. - 1) * c(1, 7, 31, 366)[i] It is calculating the end date from 31-day months. The following is correct (and even readable to those unused to such an illogical date format) in Dates in base R.> startdatetime <- as.Date("1995-01-01") > seq(as.Date("1995-01-01"), by="months", length=129)On Fri, 3 Sep 2004, Waichler, Scott R wrote:> > 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) > > beg.month.datetimes > [1] 01/01/95 02/01/95 03/01/95 04/01/95 05/01/95 06/01/95 07/01/95 > 08/01/95 > [9] 09/01/95 10/01/95 11/01/95 12/01/95 01/01/96 02/01/96 03/01/96 > 04/01/96 > [17] 05/01/96 06/01/96 07/01/96 08/01/96 09/01/96 10/01/96 11/01/96 > 12/01/96 > [25] 01/01/97 02/01/97 03/01/97 04/01/97 05/01/97 06/01/97 07/01/97 > 08/01/97 > [33] 09/01/97 10/01/97 11/01/97 12/01/97 01/01/98 02/01/98 03/01/98 > 04/01/98 > [41] 05/01/98 06/01/98 07/01/98 08/01/98 09/01/98 10/01/98 11/01/98 > 12/01/98 > [49] 01/01/99 02/01/99 03/01/99 04/01/99 05/01/99 06/01/99 07/01/99 > 08/01/99 > [57] 09/01/99 10/01/99 11/01/99 12/01/99 01/01/00 02/01/00 03/01/00 > 04/01/00 > [65] 05/01/00 06/01/00 07/01/00 08/01/00 09/01/00 10/01/00 11/01/00 > 12/01/00 > [73] 01/01/01 02/01/01 03/01/01 04/01/01 05/01/01 06/01/01 07/01/01 > 08/01/01 > [81] 09/01/01 10/01/01 11/01/01 12/01/01 01/01/02 02/01/02 03/01/02 > 04/01/02 > [89] 05/01/02 06/01/02 07/01/02 08/01/02 09/01/02 10/01/02 11/01/02 > 12/01/02 > [97] 01/01/03 02/01/03 03/01/03 04/01/03 05/01/03 06/01/03 07/01/03 > 08/01/03 > [105] 09/01/03 10/01/03 11/01/03 12/01/03 01/01/04 02/01/04 03/01/04 > 04/01/04 > [113] 05/01/04 06/01/04 07/01/04 08/01/04 09/01/04 10/01/04 11/01/04 > 12/01/04 > [121] 01/01/05 02/01/05 03/01/05 04/01/05 05/01/05 06/01/05 07/01/05 > 08/01/05 > [129] 09/01/05 10/01/05 11/01/05 > > > > Is this a bug, and can it be fixed? > > Scott Waichler > Pacific Northwest National Laboratory > scott.waichler<at>pnl.gov > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595