Bug 16802 - Cut breaks are not unique if all(x == 0)
Summary: Cut breaks are not unique if all(x == 0)
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Misc (show other bugs)
Version: R 3.2.4
Hardware: x86_64/x64/amd64 (64-bit) OS X Yosemite
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2016-04-07 14:00 UTC by joey reid
Modified: 2018-10-05 07:14 UTC (History)
2 users (show)

See Also:


Attachments
Here's a patch (541 bytes, patch)
2018-10-04 01:03 UTC, Benjamin Tyner
Details | Diff
Sorry, previous patch didn't have the abs() (546 bytes, patch)
2018-10-04 11:09 UTC, Benjamin Tyner
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description joey reid 2016-04-07 14:00:49 UTC
*Overview*
Cut uses the magnitude of the only unique value to set the sequence steps size for calculating breaks. This results in no unique breaks when that only unique value of x is 0.

*Steps to reproduce*
cut(rep(0L, 7), breaks = 3)

*Expected outcome*
Like this: cut(rep(1L, 7), breaks = 3), but centered on 0 instead of 1.

*Actual Results*
Error in cut.default(rep(0L, 7), breaks = 3) : 'breaks' are not unique


*Additional Info*
The problem is that when the range of the data is zero, cut defines a sequence using the minimum of the data as the base for the step size. 


dx <- abs(rx[1L])
breaks <- seq.int(rx[1L] - dx/1000, rx[2L] + dx/1000, length.out = nb)


Since dx = 0 when all(x == 0) the breaks will never be unique. It seems like there's no need to base the step size on the magnitude of the only value in x, just using 1/1000 would be sufficient.
Comment 1 Benjamin Tyner 2018-10-04 00:46:39 UTC
The behaviour does seem to conflict with the documentation, which says:

     "If ‘x’ is a constant vector, equal-length intervals are created, one of
     which includes the single value."
Comment 2 Benjamin Tyner 2018-10-04 01:03:48 UTC
Created attachment 2376 [details]
Here's a patch
Comment 3 Benjamin Tyner 2018-10-04 11:09:41 UTC
Created attachment 2377 [details]
Sorry, previous patch didn't have the abs()
Comment 4 Martin Maechler 2018-10-05 07:14:22 UTC
Thank you, Joey Reid, for the good report that has been dusting away undeservedly, 
and Benjamin for getting to it and propose the simple patch.

Fix committed to both R-devel (svn rev 75400) and R 3.5.1 patched