> seq(as.Date("2000-1-1"), as.Date("2001-1-1"), "months")[1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01" [6] "2000-06-01" "2000-07-01" "2000-08-01" "2000-09-01" "2000-10-01" [11] "2000-11-01" "2000-12-01" "2001-01-01"> seq(as.Date("2000-1-31"), as.Date("2001-1-31"), "months")[1] "2000-01-31" "2000-03-02" "2000-03-31" "2000-05-01" "2000-05-31" [6] "2000-07-01" "2000-07-31" "2000-08-31" "2000-10-01" "2000-10-31" [11] "2000-12-01" "2000-12-31" "2001-01-31" Is this a bug? Hadley
No, it's not a bug, it matches with the documentation. see: ?seq.POSIXt Using '"month"' first advances the month without changing the day: if this results in an invalid day of the month, it is counted forward into the next month: see the examples. So, it increases the month first, resulting in: 01-31, 02-31, 03-31, 04-31, 05-31, etc Then, for the illegal dates it counts forward. 04-31 becomes 05-01, etc Jonne. On Mon, 2007-06-04 at 09:39 +0200, hadley wickham wrote:> > seq(as.Date("2000-1-1"), as.Date("2001-1-1"), "months") > [1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01" > [6] "2000-06-01" "2000-07-01" "2000-08-01" "2000-09-01" "2000-10-01" > [11] "2000-11-01" "2000-12-01" "2001-01-01" > > > > seq(as.Date("2000-1-31"), as.Date("2001-1-31"), "months") > [1] "2000-01-31" "2000-03-02" "2000-03-31" "2000-05-01" "2000-05-31" > [6] "2000-07-01" "2000-07-31" "2000-08-31" "2000-10-01" "2000-10-31" > [11] "2000-12-01" "2000-12-31" "2001-01-31" > > Is this a bug? > > Hadley > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code.
Note that chron does give the last day of the month:> library(chron) > seq(chron("1/31/2000"), chron("1/31/2001"), "month")[1] 01/31/00 02/29/00 03/31/00 04/30/00 05/31/00 06/30/00 07/31/00 08/31/00 [9] 09/30/00 10/31/00 11/30/00 12/31/00 01/31/01 The zoo package has a "yearmon" class whose as.Date.yearmon can convert a "Date" to the end of the month: library(zoo)> library(zoo) > as.Date(as.yearmon(seq(as.Date("2000-1-31"), as.Date("2001-1-31"), "month")), 1)[1] "2000-01-31" "2000-02-29" "2000-03-31" "2000-04-30" "2000-05-31" [6] "2000-06-30" "2000-07-31" "2000-08-31" "2000-09-30" "2000-10-31" [11] "2000-11-30" "2000-12-31" "2001-01-31" And we do it ourself like this:> as.Date(format(seq(as.Date("2000-1-1"), as.Date("2001-1-1"), "month") + 32, "%Y-%m-01"))-1[1] "2000-01-31" "2000-02-29" "2000-03-31" "2000-04-30" "2000-05-31" [6] "2000-06-30" "2000-07-31" "2000-08-31" "2000-09-30" "2000-10-31" [11] "2000-11-30" "2000-12-31" "2001-01-31" On 6/4/07, hadley wickham <h.wickham at gmail.com> wrote:> > seq(as.Date("2000-1-1"), as.Date("2001-1-1"), "months") > [1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01" > [6] "2000-06-01" "2000-07-01" "2000-08-01" "2000-09-01" "2000-10-01" > [11] "2000-11-01" "2000-12-01" "2001-01-01" > > > > seq(as.Date("2000-1-31"), as.Date("2001-1-31"), "months") > [1] "2000-01-31" "2000-03-02" "2000-03-31" "2000-05-01" "2000-05-31" > [6] "2000-07-01" "2000-07-31" "2000-08-31" "2000-10-01" "2000-10-31" > [11] "2000-12-01" "2000-12-31" "2001-01-31" > > Is this a bug? > > Hadley > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >
On 6/4/07, Don MacQueen <macq at llnl.gov> wrote:> I think you can easily get the last days of the months by creating a > series starting on the first days, and subtracting one from each. > > seq(as.Date("2000-2-1"), as.Date("2001-2-1"), "months") -1Thanks for the suggestion, but for my problem the dates are user supplied so I don't know if they fall at the end of the month or not. Hadley