[R] cut and factor

Oleg Bartunov oleg at sai.msu.su
Mon Mar 29 22:24:51 CEST 2004


On Mon, 29 Mar 2004, Achim Zeileis wrote:

> On Tue, 30 Mar 2004 00:06:39 +0400 (MSD) Oleg Bartunov wrote:
>
> > Eric,
> >
> > thanks for quick reply. at first look I thought it is what I need,
> > but, unfortunately, it doesn't applied to original data - it
> > creates new data with loosing original indexes ! I want to keep
> > indexes of original data, but replace original data with $mids of
> > corresponding$breaks.
>
> Then you just don't say table() after using cut().

Aha ! Thanks a lot.



>
> > So, if I have z = 1:10, t=hist(z,plot=F)
> > > z
> >  [1]  1  2  3  4  5  6  7  8  9 10
> > > t$breaks
> > [1]  0  2  4  6  8 10
> > > t$mids
> > [1] 1 3 5 7 9
> >
> > I want z=c(1,1,3,3,5,5,7,7,9,9)
>
> and you get it via
>
> R> z <- 1:10
> R> ht <- hist(z,plot=F)
> R> z
>  [1]  1  2  3  4  5  6  7  8  9 10
> R> ht$breaks
> [1]  0  2  4  6  8 10
> R> ht$mids
> [1] 1 3 5 7 9
> R> z2 <- cut(z, ht$breaks, labels = ht$mids)
> R> z2
>  [1] 1 1 3 3 5 5 7 7 9 9
> Levels: 1 3 5 7 9
>
> or if you don't want to have a factor
>
> R> as.numeric(as.character(z2))
>  [1] 1 1 3 3 5 5 7 7 9 9
>
> best,
> Z
>
> >
> > 	Oleg
> >
> > On Mon, 29 Mar 2004, Erin Hodgess wrote:
> >
> > > Hello Oleg!
> > >
> > > Do you mean something like this, please?
> > > > z <- rnorm(15)
> > > > z
> > >  [1] -0.36888946  0.34271755 -0.47761843 -0.58402557  0.05393014
> > >  0.69234710
> > > 2.04861420  1.41823938 -0.57638598  1.51090023
> > > [11] -0.71616401  0.19227347 -0.19348506 -0.63082326 -0.64346621
> > > > z1 <- hist(z,plot=F)
> > > > z1$breaks
> > > [1] -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5
> > > > table(cut(z,z1$breaks))
> > >
> > > (-1,-0.5]  (-0.5,0]   (0,0.5]   (0.5,1]   (1,1.5]   (1.5,2]
> > > (2,2.5]
> > >         5         3         3         1         1         1
> > >         1
> > >
> > > Hope this helps!
> > > Sincerely,
> > > Erin Hodgess
> > > Associate Professor
> > > Department of Computer and Mathematical Sciences
> > > University of Houston - Downtown
> > > mailto: hodgess at gator.uhd.edu
> > >
> > >
> > >
> > > From: Oleg Bartunov <oleg at sai.msu.su>
> > >
> > >
> > > I have numeric vector z and I want to factorize it using z$breaks
> > > which I got from histograms breaks. Is there an elegant way to do
> > > this ? I, probably, could write a loop and check if z hits into some
> > > interval and replace z with value of z$mids, but I suspect there is
> > > more R-ish way.
> > >
> > > 	Regards,
> > > 		Oleg
> > > _____________________________________________________________
> > > Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
> > > Sternberg Astronomical Institute, Moscow University (Russia)
> > > Internet: oleg at sai.msu.su, http://www.sai.msu.su/~megera/
> > > phone: +007(095)939-16-83, +007(095)939-23-83
> > >
> >
> > 	Regards,
> > 		Oleg
> > _____________________________________________________________
> > Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
> > Sternberg Astronomical Institute, Moscow University (Russia)
> > Internet: oleg at sai.msu.su, http://www.sai.msu.su/~megera/
> > phone: +007(095)939-16-83, +007(095)939-23-83
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
>

	Regards,
		Oleg
_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg at sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83




More information about the R-help mailing list