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
问题1:
如果添加right = FALSE,则间隔为
(30,40)(40,50)(50,60)
如果添加right = TRUE,则间隔为
[30,40] [40,50] [50,60]
我怎样才能得到[30,40] (40,50] (50,60]
或'[30,40] [40,50] [50,60]的间隔?
问题2:
table(y)[1]
(30,40]
23
我知道间隔期间有30个数字(30,40),我可以全部用它们
x[x<=40 & x>30]
。
有没有更好的方法来获得结果?
答案 0 :(得分:1)
对于问题2,只需使用剪切的输出
x[y == "(30,40]"]
答案 1 :(得分:0)
对于你的问题#2,它取决于你对“更好”的意思。
这是一个选项:
library(TeachingDemos)
x[ 30 %<% x %<=% 40 ]
或者,您可以使用cut
:
findInterval
y <- findInterval(x, c(30,40,50,60))
x[ y==1 ]
您还可以查看subset
功能。
如果这些与“更好”的定义不符,请告诉我们您想要的更多信息。