0

creating tertiles is somehow challenging for me.

When I am using cut, the breakpoints are nicely even but the groups aren't.

When using cut2, the groups are getting more evenly but it's still not a true tertile.

ntile gives true tertiles but I can't see the break points.

DF <- data.frame(var = rnorm(1000, 50, 15))

How can I create 3 groups, have ~333 in each and get shown the breakpoints?

I'd be glad about help!

Schillerlocke
  • 305
  • 1
  • 14

1 Answers1

3

I think you're looking for quantile:

quantile(DF$var, c(0, 1/3, 2/3, 1))
#>         0%  33.33333%  66.66667%       100% 
#>  -6.379706  44.044300  56.866305 101.082961 

So you could do:

groups <- cut(DF$var, quantile(DF$var, c(0, 1/3, 2/3, 1)), include.lowest = TRUE)
table(groups)
#> groups
#> [-6.38,44]  (44,56.9] (56.9,101] 
#>        334        333        333 
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87