Reputation: 348
x<-sample(30:60,50,TRUE)
y<-cut(x,breaks=c(30,40,50,60))
y
[1] (30,40] (30,40] (50,60] (40,50] (40,50] (40,50] (40,50] (30,40] (30,40]
[10] (50,60] (30,40] (50,60] (30,40] (30,40] (50,60] (50,60] (50,60] (30,40]
[19] (50,60] (30,40] (40,50] (40,50] (30,40] (30,40] (30,40] (40,50] (30,40]
[28] (50,60] (40,50] (40,50] (30,40] (50,60] (40,50] (50,60] (50,60] (30,40]
[37] (50,60] (50,60] (30,40] (50,60] (30,40] (30,40] <NA> (40,50] (30,40]
[46] (40,50] (30,40] (30,40] (30,40] (30,40]
Levels: (30,40] (40,50] (50,60]
table(y)
y
(30,40] (40,50] (50,60]
23 12 14
table(y)[1]
(30,40]
23
Question1:
If right=FALSE is added ,the intervals are
(30,40] (40,50] (50,60]
If right=TRUE is added,the intervals are
[30,40) [40,50) [50,60)
how can i get the interval such as [30,40] (40,50] (50,60]
or '[30,40) [40,50) [50,60]`?
Question2:
table(y)[1]
(30,40]
23
I know there are 23 numbers in the interval (30,40], I can get them all with
x[x<=40 & x>30]
.
Is there any better way to get the result?
Upvotes: 0
Views: 3515
Reputation: 49640
For your question # 2 it depends on what you mean by "better".
Here is one option:
library(TeachingDemos)
x[ 30 %<% x %<=% 40 ]
Or, instead of using cut
you can use findInterval
:
y <- findInterval(x, c(30,40,50,60))
x[ y==1 ]
You could also look at the subset
function.
If these don't match your definition of "better", then tell us more about what you want.
Upvotes: 0
Reputation: 55340
For question 2, simply use the output of your cut
x[y == "(30,40]"]
Upvotes: 1