Re: [R] recategorizing a vector into discrete states

From: Achim Zeileis <Achim.Zeileis_at_wu-wien.ac.at>
Date: Wed 17 Aug 2005 - 03:48:14 EST

On Tue, 16 Aug 2005 13:18:23 -0400 Allan Strand wrote:

> Hi All,
>
> I'm trying to take a numerical vector and produce a new vector of
> the same length where each element in the first is placed into a
> category given by a 'breaks-like' vector. The values in the result
> should equal the lower bounds of each category as defined in the
> breaks vector.
>
> I suspect that a vectorized solution is pretty simple, but I can't
> seem to figure it out today. Here is an example of my problem:
>
> Vector 'a' is the original vector. Vector 'b' gives the lower bounds
> of the categories. Vector 'c' is the result I am seeking.
>
> a <- c(0.9, 11, 1.2, 2.4, 4.0, 5.0, 7.3, 8.1, 3.3, 4.5)
> b <- c(0, 2, 4, 6, 8)

>
> c <- c(0, 8, 0, 2, 4, 4, 6, 8, 2, 4)
>
> Any suggestions would be greatly appreciated.

cut(a, c(b, Inf), labels = b)

which returns a factor.
Z

> cheers,
> Allan Strand
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Aug 17 04:09:09 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:24:00 EST