[Rd] sequence of month ends using seq.Date

From: Gabor Grothendieck <ggrothendieck_at_myway.com>
Date: Sun 08 Aug 2004 - 11:38:08 EST

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.)

R-devel@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-devel Received on Sun Aug 08 11:42:28 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:45:05 EST