[R] breaks
Sundar Dorai-Raj
sundar.dorai-raj at pdf.com
Thu Jun 12 20:52:15 CEST 2003
Erin Hodgess wrote:
> Dear R People:
>
> I have a question about a "sorting" problem, please.
>
> I have a vector xx:
>
>
>>xx
>
>
> [1] -2.0 1.4 -1.2 -2.2 0.4 1.5 -2.2 0.2 -0.4 -0.9
>
> and a vector of breaks:
>
>
>>xx.y
>
>
> [1] -2.2000000 -0.9666667 0.2666667 1.5000000
>
> I want to produce another vector z which contains the number of the class
> that each data point is in.
>
> for instance, xx[1] is between xx.y[1] and xx.y[2], so z[1] == 1
>
> this can be accomplished via loops, but I was wondering if there is a more
> efficient method, please.
>
> By the way, eventually, there will be many more data points and more
> classes.
>
I think what you're looking for is ?cut:
R> xx = c(-2.0, 1.4, -1.2, -2.2, 0.4, 1.5, -2.2, 0.2, -0.4, -0.9)
R> cut(xx, breaks = c(-Inf, -2.2, -0.97, 0.27, 1.5, Inf))
[1] (-2.2,-0.97] (0.27,1.5] (-2.2,-0.97] (-Inf,-2.2] (0.27,1.5]
[6] (0.27,1.5] (-Inf,-2.2] (-0.97,0.27] (-0.97,0.27] (-0.97,0.27]
Levels: (-Inf,-2.2] (-2.2,-0.97] (-0.97,0.27] (0.27,1.5] (1.5,Inf]
R>
Regards,
Sundar
More information about the R-help
mailing list