[Rd] sequence of month ends using seq.Date

Gabor Grothendieck ggrothendieck at myway.com
Sun Aug 8 03:38:08 CEST 2004


It seems that seq.Date starting from month end will always give d days past
the beginning of each month (if the month end of the first month is d days
after the first of that month).  This has the effect of jumping into the
following month for short months. For example, the 3 months after
Jan 31, 2004 are Mar 2, 2004, Mar 31, 2004 and May 1, 2004 according
to seq.Date:

R> seq(as.Date("2004-01-31"), length = 4, by = "month")
[1] "2004-01-31" "2004-03-02" "2004-03-31" "2004-05-01"

In contrast chron uses these rules:

- if the first date is near the end of the month and the sequence
  would otherwise skip a month at any point then use the last day
  of the month, as shown below.

- if the first date is the end of its month then use month ends.  Thus
  starting at Jan 31, 2004 or starting at Feb 29, 2004 would both
  give sequences of month ends.

Here is the calculation using chron:

R> require(chron)
R> seq(chron("01/31/04"), length = 4, by = "month")
[1] 01/31/04 02/29/04 03/31/04 04/30/04

Ideally one would be able to specify to seq.Date whether one wants 
counting done from the beginning of each month (to get the current effect)
or the end of each month (to easily get end of month or second day to the 
end of month sequences, etc.) with reasonable defaults.

(At the moment, workarounds to get end of month sequences
include performing the seq in chron and converting to Date or 
doing a seq relative to the first of the NEXT month and subtracting 
one to get end of month.)



More information about the R-devel mailing list