将连续变量分成相等大小的组

时间:2019-04-12 10:50:20

标签: r split cut bin

我有一个连续变量Total_testscore。我将其分为4组,但它们的样本量不相等。如何拆分变量,以使变量的结果级别具有相同的样本量?

> mydata$Total_testscore_f <- cut(mydata$Total_testscore, breaks = 4)
> table(mydata$Total_testscore_f)

(1.99,5.5]    (5.5,9]   (9,12.5]  (12.5,16] 
        25        116         95         50 

1 个答案:

答案 0 :(得分:0)

您可以使用quantile()函数来获取所需的休息时间。示例:

> x <- rnorm(100) # replace with your real data
> table(cut(x, breaks = quantile(x, seq(0, 1, l=5)), include.lowest = TRUE))

 [-2.07,-0.756] (-0.756,0.0664]  (0.0664,0.963]    (0.963,2.83] 
             25              25              25              25