织梦CMS - 轻松建站从此开始!

abg欧博官网|登陆|游戏|

7.1 Recoding a Continuous Variable to a Categorica

时间:2025-09-13 02:00来源: 作者:admin 点击: 6 次
This cookbook contains more than 150 recipes to help scientists, engineers, programmers, and data analysts generate high-quality graphs quickly—withou

For three categories we specify four bounds, which can include Inf and -Inf. If a data value falls outside of the specified bounds, it’s categorized as NA. The result of cut() is a factor, and you can see from the example that the factor levels are named after the bounds.

To change the names of the levels, set the labels:

pg$wtclass <- cut(pg$weight, breaks = c(0, 5, 6, Inf), labels = c("small", "medium", "large")) pg #> weight group wtclass #> 1 4.17 ctrl small #> 2 5.58 ctrl medium #> ...<26 more rows>... #> 29 5.80 trt2 medium #> 30 5.26 trt2 medium

As indicated by the factor levels, the bounds are by default open on the left and closed on the right. In other words, they don’t include the lowest value, but they do include the highest value. For the smallest category, you can have it include both the lower and upper values by setting include.lowest=TRUE. In this example, this would result in 0 values going into the small category; otherwise, 0 would be coded as NA.

If you want the categories to be closed on the left and open on the right, set right = FALSE:

cut(pg$weight, breaks = c(0, 5, 6, Inf), right = FALSE) #> [1] [0,5) [5,6) [5,6) [6,Inf) [0,5) [0,5) [5,6) [0,5) [5,6) #> [10] [5,6) [0,5) [0,5) [0,5) [0,5) [5,6) [0,5) [6,Inf) [0,5) #> [19] [0,5) [0,5) [6,Inf) [5,6) [5,6) [5,6) [5,6) [5,6) [0,5) #> [28] [6,Inf) [5,6) [5,6) #> Levels: [0,5) [5,6) [6,Inf)

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-09-18 18:09 最后登录:2025-09-18 18:09
栏目列表
推荐内容