[R] Cut breaks in descending order

David Winsemius dwinsemius at comcast.net
Fri Apr 3 18:34:46 CEST 2015


On Apr 3, 2015, at 5:09 AM, Wing Keong Lew wrote:

> Hi,
> 
> Is it a requirement to provide the break intervals of the cut function in ascending order?

Apparently not. I get teh sam splits even with random permutations. It is apparently a "requirement" to make sure you labels match the sorted order of the breaks.

The findInterval function does require that its `vec` argument be non-decreasing, but I do not see a discussion of break order in the help page. Looking at cut.default the first think it does to the breaks is sort them.

  #... snipped code that deals with length(breaks)==1
   else nb <- length(breaks <- sort.int(as.double(breaks)))

-- 
David.

> The help documentation for cut didn't specify this but the labels returned are reversed if I indicate the break intervals in a descending order. Here is an example
> 
> tbl<-data.frame(x=c(0:10))
> tbl$ascending<-cut(tbl$x, breaks=c(0,3,5,9,99), labels=c('<3','4-5','6-9','>9'), include.lowest=T)
> tbl$descending<-cut(tbl$x, breaks=c(99,9,5,3,0), labels=c('>9','6-8','4-5','<3'), include.lowest=T)
> tbl
>     x ascending descending
> 1   0        <3        >9
> 2   1        <3        >9
> 3   2        <3        >9
> 4   3        <3        >9
> 5   4       4-5       6-8
> 6   5       4-5       6-8
> 7   6       6-9       4-5
> 8   7       6-9       4-5
> 9   8       6-9       4-5
> 10  9       6-9       4-5
> 11 10        >9        <3
> 
> Appreciate any guidance on this.
> Regards
> Wing Keong
> 
> 		 	   		  
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list