Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
For the examples here, we’ll use a subset of the PlantGrowth data set.
# Work on a subset of the PlantGrowth data setpg<-PlantGrowth[c(1,2,11,21,22),]pgweight group4.17ctrl5.58ctrl4.81trt16.31trt25.12trt2
In this example, we’ll recode the continuous variable weight into a categorical variable, wtclass, using the cut()
function:
pg$wtclass<-cut(pg$weight,breaks=c(0,5,6,Inf))pgweight group wtclass4.17ctrl(0,5]5.58ctrl(5,6]4.81trt1(0,5]4.17trt1(0,5]6.31trt2(6,Inf]5.12trt2(5,6]