# Re: [R] cut in R

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Thu 21 Jul 2005 - 18:04:57 EST

"Steve Su" <s.su@qut.edu.au> writes:

> Dear All,
>
> I wonder whether it is still valid to use the following R code for cut. All I have done is changed:
>
> if (is.na(breaks) | breaks < 2)
>
> to:
>
> if (is.na(breaks) | breaks < 1)
>
> so that it covers interval of 1?
>
> It seems okay for my purposes but I am not sure why R specifically does not allow break<2 to happen.
>
> Steve.

What do you need it for? It gives you a factor with only one group, so I suppose that the idea is that this is more likely to be due to a programming error.

(However, I spot a bit of a bug in that we don't set include.lowest=TRUE when using breaks as a number:

> x <- round(rnorm(20),2)
> x
[1] 0.66 -2.22 -0.70 -1.68 0.38 -0.23 -0.43 -0.72 0.30 -0.22 -1.36
> 0.60

[13] 0.44 -0.40 -0.61 1.08 -0.41 -0.02 -1.41 -0.49
> cut(x,breaks=3)

``` [1] (-0.0189,1.08]  (-2.22,-1.12]   (-1.12,-0.0189] (-2.22,-1.12]
[5] (-0.0189,1.08]  (-1.12,-0.0189] (-1.12,-0.0189] (-1.12,-0.0189]
[9] (-0.0189,1.08]  (-1.12,-0.0189] (-2.22,-1.12]   (-0.0189,1.08]
```
[13] (-0.0189,1.08] (-1.12,-0.0189] (-1.12,-0.0189] (-0.0189,1.08] [17] (-1.12,-0.0189] (-1.12,-0.0189] (-2.22,-1.12] (-1.12,-0.0189] Levels: (-2.22,-1.12] (-1.12,-0.0189] (-0.0189,1.08]

Notice how -2.22 appears to be inside the interval (-2.22,-1.12] .)

```--
O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help