【描述】
cut divides the range of x into intervals and codes the values in x according to which interval they fall. The leftmost interval corresponds to level one, the next leftmost to level two and so on.
cut將x的范圍劃分為若干個區(qū)間恼除,并根據(jù)這些區(qū)間對x中的值進行編碼修赞。最左邊的區(qū)間對應(yīng)于第一級腰根,最左邊的區(qū)間對應(yīng)于第二級均抽,依此類推邻辉。
個人感覺最重要的一點在于生成劃分因子
【用法】
cut(x, ...)
## Default S3 method:
cut(x, breaks, labels = NULL,
include.lowest = FALSE, right = TRUE, dig.lab = 3,
ordered_result = FALSE, ...)
【參數(shù)】
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.
【代碼】
> cut(1:10, breaks = seq(0, 10, 5)) # 默認(rèn)情況下左開右閉
[1] (0,5] (0,5] (0,5] (0,5] (0,5] (5,10] (5,10] (5,10] (5,10] (5,10]
Levels: (0,5] (5,10]
# break 可以為自定義的分組也可以為大于等于2的數(shù)字习瑰,如果是數(shù)字琼懊,則軟件會自動均分?jǐn)?shù)值間的距離熏版,如果不想均分,可以自定義分類距離
> cut(1:10, breaks = c(0, 3, 5, 8, 10))
[1] (0,3] (0,3] (0,3] (3,5] (3,5] (5,8] (5,8] (5,8] (8,10] (8,10]
Levels: (0,3] (3,5] (5,8] (8,10]
# right 代表區(qū)間的左右端開和閉 默認(rèn)為true氯夷,代表左開右閉臣樱,當(dāng)設(shè)置成False的時候,為左閉右開
# label 為標(biāo)簽向量腮考,代表給每段間距設(shè)置一個標(biāo)簽雇毫,相當(dāng)于每個間距給一個名字
> cut(1:10, breaks = c(1, 3, 5, 8, 11), right = F, labels = c('A', 'B', 'C', 'D'))
[1] A A B B C C C D D D
Levels: A B C D