Use Cut in R to turn numeric into factors

Use cut to turn numeric into factors, click HERE for more info about cut.

The flag you might be interested will the breaks=:

If you only pass one number to that flag, it will divide the whole range into equivalent intervals, like the example I showed below. You can also pass a vector of number which will specify how the interval will be divided.

data(mtcars) library(plyr) mydata <- mtcars # Here I cut the whole numeric range into 10 equal intervals mydata$myhp <- cut(mydata$hp, 10) # Here is how the data looks like: mpg cyl disp hp drat wt qsec vs am gear carb myhp Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 (108,137] Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 (108,137] Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 (80.1,108] Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 (108,137] Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 (165,194] Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 (80.1,108] Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 (222,250] Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 (51.7,80.1] > str(mydata) 'data.frame': 32 obs. of 12 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... .... $ myhp: Factor w/ 10 levels "(51.7,80.1]",..: 3 3 2 3 5 2 7 1 2 3 ...
 
http://stackoverflow.com/questions/20365847/binary-logistic-regression-using-r 

你可能感兴趣的:(Use Cut in R to turn numeric into factors)