在R語言中,可以使用cut函數(shù)將一個數(shù)值向量分成幾個離散的組。cut函數(shù)的語法如下:
cut(x, breaks, labels = NULL, include.lowest = FALSE, right = TRUE, dig.lab = 3)
參數(shù)說明:
以下是一個使用cut函數(shù)分組的示例:
# 創(chuàng)建一個數(shù)值向量
x <- c(10, 23, 35, 45, 52, 67, 73, 85, 91, 100)
# 使用cut函數(shù)分成三個組
groups <- cut(x, breaks = 3)
# 輸出結(jié)果
groups
運行上述代碼會將x分成三個組,并將結(jié)果存儲在groups變量中。輸出結(jié)果如下:
[1] (9.93,37.3] (9.93,37.3] (37.3,64.7] (37.3,64.7] (37.3,64.7] (64.7,92] (64.7,92] (64.7,92] (92,100] (92,100]
Levels: (9.93,37.3] (37.3,64.7] (64.7,92] (92,100]
可以看到,cut函數(shù)將x分成了四個組,每個組由一個區(qū)間表示,并將結(jié)果以因子的形式返回。