[R] breaks

Wiener, Matthew matthew_wiener at merck.com
Thu Jun 12 21:00:21 CEST 2003



t1 <- outer(data, breaks + c(rep(0, length(breaks)-1), 1e-5), "<")
Apply(t1, 1, function(x){min(which(x))}) - 1

Adding to the final break point makes sure that every data point will be
less than some break point.

So:

> xx <- c(-2, 1.4, -1.2, -2.2, .4, 1.5, -2.2, 0.2, -.4, -.9)
> xx.y <- c(-2.2, -0.967, 0.2667, 1.5)
> t1 <- outer(xx, xx.y + c(rep(0, length(xx.y)-1), 1), "<")
> apply(t1, 1, function(x){min(which(x))}) - 1
 [1] 1 3 1 1 3 3 1 2 2 2
> 


Hope this helps,

Matt


-----Original Message-----
From: Erin Hodgess [mailto:hodgess at uhddx01.dt.uh.edu] 
Sent: Thursday, June 12, 2003 2:34 PM
To: r-help at stat.math.ethz.ch
Subject: [R] breaks


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.

thank you for any help!

sincerely,
Erin Hodgess
mailto: hodgesse at uhd.edu

Version 1.7.0 R for Windows

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

------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, cont... {{dropped}}




More information about the R-help mailing list