R – Using cut2 from Hmisc to calculate cuts for different number of groups

hmiscquantiler

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

But I was expecting the following result (33%, 33%, 33%):

[1] -4.18304 -2.13478 -1.74858 -0.56810

Should I still use cut2 or try something different? How can I make it work? Thanks for your advice.

Best Solution

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 

(Obviously you cannot expect cut2 to create an exact split when the count of elements was not evenly divisible by 3.)

Related Question