cut2分成不等的桶

时间:2016-02-08 20:52:57

标签: r

我目前正在进行一些数据操作,并且一直在寻找一种方法来创建每组中具有相同观察次数的十分位数。我遇到了Hmisc软件包和cut2函数,并且在印象中它应该将数据拆分成10个桶,每个桶中具有相同数量的观察值,通过指定g = 10。但是这个功能的输出已经相当多了。我是否正确使用cut2?

我正在使用的代码:

library(Hmisc)
testdata <- data.frame(rating= c(8, 8,  8,  6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  4,  8,  8,  8,  6,  8,  8,  8,  8,  6,  8,  6,  8,  4,  8,  8,  8,  6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  4,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  6,  8,  8,  8,  8,  6,  6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  6,  8,  6,  8,  8,  8,  8,  6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  6,  8,  8,  8,  6,  8,  8,  6,  4,  8,  8,  8,  8,  8,  6,  8,  8,  8,  4,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  2,  8,  6,  8,  8,  8,  6,  8,  8,  6,  6,  8,  8,  6,  8,  8,  8,  8,  8,  8,  8,  8,  6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  4,  8,  8,  8,  6,  8,  8,  6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  4,  8,  6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  6,  6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  4,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  6,  8,  6,  8,  8,  8,  6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  6,  8,  8,  8,  8,  8,  6,  8,  8,  8,  6)
,age=c(0,   0,  0,  0,  3,  4,  4,  4,  4,  6,  6,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9,  9,  9,  10, 10, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30, 30, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33, 34, 34, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, 44, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 48, 48, 48, 54, 54, 54, 56, 56, 58, 59, 59, 59, 59, 60, 60, 60, 61, 66, 66, 70, 72))
cutcutcut <- cut2(testdata$age,g=10)
testtable <- table(cutcutcut)

和每个桶中不等观察的输出

testtable

 [ 0,13) [13,15) [15,20) [20,24) [24,26) [26,28) [28,33) [33,40) [40,46) [46,72] 
 46      16      35      28      33      35      26      31      31      28 

1 个答案:

答案 0 :(得分:0)

您的问题的答案在于查看数据的分布:

table(testdata$age)
#  0  3  4  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 
#  4  1  4  6  4  3  4  2  2 16  9  7  5 10  6  7  7 13  4  2  9 
# 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 
# 23 10 18 17  8  5  3  2  8  2  2  5  9  5  5  3  2  8  7  3  6 
# 45 46 47 48 54 56 58 59 60 61 66 70 72 
#  5  4  3  3  3  2  1  4  3  1  2  1  1 

我们发现有些年龄段的人在该年龄段有很多人(例如,有16个年龄在12岁的人和23个年龄在24岁的人)。由于切割算法需要将具有完全相同年龄的所有个体放入同一个桶中,这可能会导致桶中的一些不平衡。

由于您的数据中总共有309个观察值并且您要搜索10个桶,因此理想情况下,您需要在9个桶中进行31个观察,在最后进行30个观察。现在,最后一个桶定义为[46, 72],其中包含28个元素(太低)。如果将其扩展为[45, 72],则它将包含33个元素(太多)。由于有5个元素值为45,因此无法将数据拆分为在最后一个桶中获得30或31个观测值。