对于一组连续变量数据,进行分类,如何操作呢。方法很多种,现介绍一种比较简单的方法。
例子如下。
mydata<-1:20
myresult<-cut(mydata, c(0,5,15,20))
myresult
[1] (0,5] (0,5] (0,5] (0,5] (0,5] (5,15] (5,15] (5,15] (5,15] (5,15] (5,15] (5,15] (5,15] (5,15]
[15] (5,15] (15,20] (15,20] (15,20] (15,20] (15,20]
Levels: (0,5] (5,15] (15,20]
mydata<-1:20
########################分类后标上标签
myresult<-cut(mydata, c(0,5,15,20), labels = c("Young", "Middle-aged","Senior"))
myresult
[1] Young Young Young Young Young Middle-aged Middle-aged Middle-aged Middle-aged
[10] Middle-aged Middle-aged Middle-aged Middle-aged Middle-aged Middle-aged Senior Senior Senior
[19] Senior Senior
Levels: Young Middle-aged Senior
myresult<-ordered(cut(mydata, c(0,5,15,20), labels = c("Young", "Middle-aged","Senior")))
myresult
[1] Young Young Young Young Young Middle-aged Middle-aged Middle-aged Middle-aged
[10] Middle-aged Middle-aged Middle-aged Middle-aged Middle-aged Middle-aged Senior Senior Senior
[19] Senior Senior
Levels: Young < Middle-aged < Senior
仅供参考 大家各抒己见
|