[R] summary to breaks question
Erin Hodgess
hodgess at uhddx01.dt.uh.edu
Thu Jun 12 21:20:05 CEST 2003
Dear R People:
thanks SO MUCH for the quick responses to the breaks questions.
Here are two possible solutions:
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)
andR> 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>
Status: R
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
>
Thanks to Rolf, Sundar, and Matthew!!!!!!
Sincerely,
Erin
More information about the R-help
mailing list