# 生成年龄向量
age = c(16, 20, 23, 25, 28, 30, 33, 36)
# 设置分割点,假定分成四个组即:
# [0,18) [18,25) [25,29) [29,40)
breaks = c(0, 18, 25, 29, 40)
# 验证分组结果
age_group_test = cut(age, breaks, right = FALSE)
print(age_group_test)
# 将年龄向量转换为因子向量
age_group = cut(
age, breaks, right = FALSE,
labels = c("1", "2", "3", "4")
)
print(age_group)