介绍
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