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