R语言 把一个向量分成若干范围 – cut()函数
R语言中的 cut() 函数是用来将一个数字向量划分为不同的范围。
语法:
cut.default(x, breaks, labels = NULL, include.below = FALSE, right = TRUE, dig.lab = 3)
参数:
x: 数字向量
break: 向量的断点
labels: 水平的标签
include.lower: 布尔值,包括最低的断点值
right: 布尔值,在右边关闭区间
dig.lab: 在没有提供标签时使用
例子1 :
# R program to divide vector into ranges
# Generating a vector with random numbers
y <- rnorm(100)
# the output factor is created by the division
# of the range of variables into pi / 3*(-3:3)
# 4 equal-length intervals
table(cut(y, breaks = pi / 3*(-3:3)))
输出
(-3.14, -2.09] (-2.09, -1.05] (-1.05, 0] (0, 1.05] (1.05, 2.09]
0 12 33 40 12
(2.09, 3.14]
2
例2 :
# R program to divide vector into ranges
# Creating vectors
age <- c(40, 49, 48, 40, 67, 52, 53)
salary <- c(103200, 106200, 150200, 10606, 10390, 14070, 10220)
gender <- c("male", "male", "transgender",
"female", "male", "female", "transgender")
# Creating data frame named employee
employee<- data.frame(age, salary, gender)
# Creating a factor corresponding to age with labels
wfact = cut(employee$age, 3, labels = c('Young', 'Medium', 'Aged'))
table(wfact)
输出
wfact
Young Medium Aged
4 2 1