From marc_schwartz at comcast.net  Thu Mar 20 22:14:35 2008
From: marc_schwartz at comcast.net (Marc Schwartz)
Date: Thu, 20 Mar 2008 16:14:35 -0500
Subject: [Rd] cut.Date and cut.POSIXt problem
In-Reply-To: <47E010CD.6090200@jhsph.edu>
References: <971536df0803181004x5ebbe960q6879506089b3e428@mail.gmail.com>
	<47E010CD.6090200@jhsph.edu>
Message-ID: <47E2D3BB.2080106@comcast.net>

Hi all,

Apologies for the delay in my engaging in this thread. I was traveling 
this week.

The problem that Gabor raised was caused by the patch that I submitted 
to fix a problem with the referenced functions when using 'months' and 
'years' as the interval. The prior versions were problematic:

   https://stat.ethz.ch/pipermail/r-devel/2008-January/048004.html

The patch fixed the error, but since I used hist.Date() as the reference 
model and did not note the subtle difference in cut.Date() relative to 
specifying the breaks increment value, this functionality was lost when 
the same modification was made to the code in cut.Date().

Roger's patch helps, but does not totally remedy the situation. One also 
needs to modify the method used for specifying the max value 'end' for 
the breaks in order to include the max 'x' Date value in the result.

Hence, I am attaching proposed patches against R-devel for 
base:::dates.R and base:::datetime.R.

I am also attaching a patch for tests:::reg-tests-1.R to add a check for 
this situation to the regression tests that were also added subsequent 
to that prior set of patches that I had submitted.

If perhaps Roger and Gabor could so some testing on these patches before 
they are considered for inclusion into the R-devel tree, it would be 
helpful to check to see if I have missed something else here.

Thanks for raising this issue.

Regards,

Marc Schwartz

Roger D. Peng wrote:
> Seems changes in r44116 force the interval to be single months (or 
> years) instead of whatever the user specified.  I think the attached 
> patches correct this.
> 
> Interestingly, 'cut' and 'seq' allow for the 'breaks' specification to 
> be something like "3 months" but the documentation for 'hist' does not 
> allow for this type of specification.
> 
> -roger
> 
> Gabor Grothendieck wrote:
>> cut.Date and cut.POSIXt indicate that the breaks argument
>> can be an integer followed by a space followed by "year", etc.
>> but it seems the integer is ignored.
>>
>> For example, I assume that breaks = "3 months" is supposed
>> to cut it into quarters but, in fact, it cuts it into months as if
>> 3 had not been there.
>>
>>> d <- seq(Sys.Date(), length = 12, by = "month")
>>> cut(d, "3 months")
>>  [1] 2008-03-01 2008-04-01 2008-05-01 2008-06-01 2008-07-01 2008-08-01
>> 2008-09-01 2008-10-01 2008-11-01 2008-12-01 2009-01-01 2009-02-01
>> Levels: 2008-03-01 2008-04-01 2008-05-01 2008-06-01 2008-07-01
>> 2008-08-01 2008-09-01 2008-10-01 2008-11-01 2008-12-01 2009-01-01
>> 2009-02-01
>>> cut(as.POSIXct(d), "3 months")
>>  [1] 2008-03-01 2008-04-01 2008-05-01 2008-06-01 2008-07-01 2008-08-01
>> 2008-09-01 2008-10-01 2008-11-01 2008-12-01 2009-01-01 2009-02-01
>> Levels: 2008-03-01 2008-04-01 2008-05-01 2008-06-01 2008-07-01
>> 2008-08-01 2008-09-01 2008-10-01 2008-11-01 2008-12-01 2009-01-01
>> 2009-02-01
>>> cut(as.POSIXlt(d), "3 months")
>>  [1] 2008-03-01 2008-04-01 2008-05-01 2008-06-01 2008-07-01 2008-08-01
>> 2008-09-01 2008-10-01 2008-11-01 2008-12-01 2009-01-01 2009-02-01
>> Levels: 2008-03-01 2008-04-01 2008-05-01 2008-06-01 2008-07-01
>> 2008-08-01 2008-09-01 2008-10-01 2008-11-01 2008-12-01 2009-01-01
>> 2009-02-01
>>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dates.R.patch
Type: text/x-patch
Size: 1082 bytes
Desc: not available
Url : https://stat.ethz.ch/pipermail/r-devel/attachments/20080320/0de154b9/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: datetime.R.patch
Type: text/x-patch
Size: 1037 bytes
Desc: not available
Url : https://stat.ethz.ch/pipermail/r-devel/attachments/20080320/0de154b9/attachment-0001.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: reg-tests-1.R.patch
Type: text/x-patch
Size: 1935 bytes
Desc: not available
Url : https://stat.ethz.ch/pipermail/r-devel/attachments/20080320/0de154b9/attachment-0002.bin 


[Rd] cut.Date and cut.POSIXt problem
Marc Schwartz