Re: [Rd] seq.Date bug?

From: Marc Schwartz <marc_schwartz_at_me.com>
Date: Tue, 31 Jan 2012 14:20:46 -0600

On Jan 31, 2012, at 2:07 PM, Sarah Goslee wrote:

> I was prompted to try it myself:
>
> On Tue, Jan 31, 2012 at 2:56 PM, Dirk Eddelbuettel <edd_at_debian.org> wrote:

>> 
>> R> seq(as.Date(Sys.Date()), by="-1 months", length=6)
>> [1] "2012-01-31" "2011-12-31" "2011-12-01" "2011-10-31" "2011-10-01" "2011-08-31"
>> R>
>> 
>> Notice how October appears twice.

>
> As does December.
>
>> Now, date arithmetic is gruesome but the documentation for seq.Date et al
>> does not hint it wouldn't honour the by= argument.  So a bug, or merely a
>> somewhat less than desirable features.

>
> The by argument chokes on "month" if the current day is greater than the
> shortest month in the sequence (presumably due to the irregular nature
> of month lengths):
>
> For leap year 2012:
>> seq(as.Date("2012/1/29"), by="month", length.out=12) # works

> [1] "2012-01-29" "2012-02-29" "2012-03-29" "2012-04-29" "2012-05-29"
> [6] "2012-06-29" "2012-07-29" "2012-08-29" "2012-09-29" "2012-10-29"
> [11] "2012-11-29" "2012-12-29"
>> seq(as.Date("2012/1/30"), by="month", length.out=12) # fails

> [1] "2012-01-30" "2012-03-01" "2012-03-30" "2012-04-30" "2012-05-30"
> [6] "2012-06-30" "2012-07-30" "2012-08-30" "2012-09-30" "2012-10-30"
> [11] "2012-11-30" "2012-12-30"
>
> While for non-leap year 2011:
>> seq(as.Date("2011/1/28"), by="month", length.out=12) # works

> [1] "2011-01-28" "2011-02-28" "2011-03-28" "2011-04-28" "2011-05-28"
> [6] "2011-06-28" "2011-07-28" "2011-08-28" "2011-09-28" "2011-10-28"
> [11] "2011-11-28" "2011-12-28"
>> seq(as.Date("2011/1/29"), by="month", length.out=12) #fails

> [1] "2011-01-29" "2011-03-01" "2011-03-29" "2011-04-29" "2011-05-29"
> [6] "2011-06-29" "2011-07-29" "2011-08-29" "2011-09-29" "2011-10-29"
> [11] "2011-11-29" "2011-12-29"

The issue is the if the next month in sequence does not contain the date, then the date is advanced until the next valid date. For example:

> seq.Date(as.Date("2012/01/30"), by = "month", length.out = 3)
[1] "2012-01-30" "2012-03-01" "2012-03-30"

February 30th does not exist, thus that date is advanced to March 1st, then the next date in the sequence is March 30th. Thus, two days in March.

> seq.Date(as.Date("2012/10/31"), by = "month", length.out = 3)
[1] "2012-10-31" "2012-12-01" "2012-12-31"

Here, November 31st does not exist, so the date is advanced to the next valid date, December 1 and then the next date is December 31. Thus, two days in December.

So it appears to be working correctly.

HTH, Marc Schwartz



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 31 Jan 2012 - 20:24:06 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 31 Jan 2012 - 20:50:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive