使用Hmisc中的cut2来计算不同数量的组的切割 [英] Using cut2 from Hmisc to calculate cuts for different number of groups
本文介绍了使用Hmisc中的cut2来计算不同数量的组的切割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过使用Hmisc的cut2为向量计算相等的分位数.
I was trying to calculate equal quantile cuts for a vector by using cut2 from Hmisc.
library(Hmisc)
c <- c(-4.18304,-3.18343,-2.93237,-2.82836,-2.13478,-2.01892,-1.88773,
-1.83124,-1.74953,-1.74858,-0.63265,-0.59626,-0.5681)
cut2(c, g=3, onlycuts=TRUE)
[1] -4.18304 -2.01892 -1.74858 -0.56810
但是我期望得到以下结果(33%,33%,33%):
But I was expecting the following result (33%, 33%, 33%):
[1] -4.18304 -2.13478 -1.74858 -0.56810
我还是应该使用cut2还是尝试其他方法?我该如何运作?谢谢你的建议.
Should I still use cut2 or try something different? How can I make it work? Thanks for your advice.
推荐答案
您看到的是切点,但您希望表格化计数,并且希望它们占总数的一部分,因此,请执行以下操作:
You are seeing the cutpoints, but you want the tabular counts, and you want them as fractions of the total, so do this instead:
> prop.table(table(cut2(c, g=3) ) )
[-4.18,-2.019) [-2.02,-1.749) [-1.75,-0.568]
0.3846154 0.3076923 0.3076923
(很明显,当元素的数量不能被3整除时,您不能期望cut2能够创建精确的拆分.)
(Obviously you cannot expect cut2 to create an exact split when the count of elements was not evenly divisible by 3.)
这篇关于使用Hmisc中的cut2来计算不同数量的组的切割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文