R语言中的函数13:cut()

介绍

cut()函数通过将一个连续变量分割后形成因子变量

用法

cut(x, breaks, labels = NULL,  include.lowest = FALSE, right = TRUE, dig.lab = 3,
    ordered_result = FALSE, ...)

参数介绍

  • x
    a numeric vector which is to be converted to a factor by cutting.

  • breaks
    either a numeric vector of two or more unique cut points or a single number (greater than or equal to 2) giving the number of intervals into which x is to be cut.

  • labels
    labels for the levels of the resulting category. By default, labels are constructed using “(a,b]” interval notation. If labels = FALSE, simple integer codes are returned instead of a factor.

  • include.lowest
    logical, indicating if an ‘x[i]’ equal to the lowest (or highest, for right = FALSE) ‘breaks’ value should be included.

  • right
    logical, indicating if the intervals should be closed on the right (and open on the left) or vice versa.

  • dig.lab
    integer which is used when labels are not given. It determines the number of digits used in formatting the break numbers.

  • ordered_result
    logical: should the result be an ordered factor?


  • further arguments passed to or from other methods.

实例

Z <- stats::rnorm(20)
cut(Z, breaks = -6:6)
# [1] (-1,0]  (-1,0]  (-1,0]  (-2,-1] (-1,0]  (1,2]   (-1,0]  (1,2]   (0,1]   (-2,-1] (0,1]   (-1,0]  (0,1]  
# [14] (-1,0]  (0,1]   (0,1]   (-1,0]  (-1,0]  (0,1]   (-1,0] 
# Levels: (-6,-5] (-5,-4] (-4,-3] (-3,-2] (-2,-1] (-1,0] (0,1] (1,2] (2,3] (3,4] (4,5] (5,6]
table(cut(Z, breaks = -6:6))
# (-6,-5] (-5,-4] (-4,-3] (-3,-2] (-2,-1]  (-1,0]   (0,1]   (1,2]   (2,3]   (3,4]   (4,5]   (5,6] 
# 0       0       0       0       2      10       6       2       0       0       0       0 
table(cut(Z, breaks = -6:6,labels=letters[1:12]))
# a  b  c  d  e  f  g  h  i  j  k  l 
# 0  0  0  0  2 10  6  2  0  0  0  0 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值