R 函数学习 - cut()

2020-03-28  本文已影响0人  Thinkando

如有1000个学生的考试分数(满分100),想要按照分数评等级,
如果是均匀划分(90-100,80-90,70-80,...),直接做除法取商就行,但如果划分不均匀(90-100,75-90,60-75,0-60), cut函数将会派上用场。

cut(vector,cuttime,labels=F)

> c0=c(2.3,3,1.5,4.8,9,3,4,7,8,9.5)  #被划分的对象
> cuttime=c(0,3,6,9,10)              #分割点
> cut(c0,cuttime,labels = F,right = F)  #左闭右开
 [1] 1 2 1 2 4 2 2 3 3 4

> cut(c0,cuttime,right=F) # 查看标签
 [1] [0,3)  [3,6)  [0,3)  [3,6)  [9,10) [3,6)  [3,6)  [6,9)  [6,9)  [9,10)
Levels: [0,3) [3,6) [6,9) [9,10)

#note2#
#include.lowest:第一个区间包含左端点/最后一个区间包含右端点
> cut(c0,cuttime,include.lowest=T)
 [1] [0,3]  [0,3]  [0,3]  (3,6]  (6,9]  [0,3]  (3,6]  (6,9]  (6,9]  (9,10]
Levels: [0,3] (3,6] (6,9] (9,10]

> cut(c0,cuttime,right=F,include.lowest=T)
 [1] [0,3)  [3,6)  [0,3)  [3,6)  [9,10] [3,6)  [3,6)  [6,9)  [6,9)  [9,10]
Levels: [0,3) [3,6) [6,9) [9,10]

#note3#
#label=F:只返回在第几个区间,否则会显示具体的区间
> cut(c0,cuttime)
 [1] (0,3]  (0,3]  (0,3]  (3,6]  (6,9]  (0,3]  (3,6]  (6,9]  (6,9]  (9,10]
Levels: (0,3] (3,6] (6,9] (9,10]

#note4#
#cuttime:一般加0/min和最大值;否则显示na,n个切点有n-1个区间
> cuttime=c(3,6,9)
> cut(c0,cuttime,labels = F)
 [1] NA NA NA  1  2 NA  1  2  2 NA
上一篇 下一篇

猜你喜欢

热点阅读