R语言数据基础离散化(分箱)之——手动定义间断点

R语言本身提供手动定义间断点进行离散化的函数 cut

 

cut(x, breaks, labels = NULL,

      include.lowest = FALSE, right = TRUE, 

      dig.lab = 3, ordered_result = FALSE, ...)  

示例代码如下:

library(infotheo)

data.restable <- read.csv('data/ChinaFeatures.csv')
res <- cut(data.restable$lat,c(18.7,23.75,28.75,33.75),c(1:3))

其将data.restable表lat列离散化,数值范围为

(18.7-23.75] ——> 1

(23.75-28.75] ——> 2

(28.75-33.75] ——> 3

注意其边界,含右不含左。