[R] cut in R

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Jul 21 10:04:57 CEST 2005


"Steve Su" <s.su at 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 at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list