Equal frequency tries to put the same quantity of cases per bin when possible. It's a wrapper of function cut2 from Hmisc package.

equal_freq(var, n_bins)

Arguments

var

input variable

n_bins

number of bins to split 'var' by equal frequency, if it not possible to calculate for the desired bins, it returns the closest number

Value

The binned variable.

Examples

## Example 1 summary(heart_disease$age)
#> Min. 1st Qu. Median Mean 3rd Qu. Max. #> 29.00 48.00 56.00 54.44 61.00 77.00
age_2=equal_freq(var=heart_disease$age, n_bins = 10) summary(age_2)
#> [29,43) [43,46) [46,51) [51,54) [54,57) [57,59) [59,61) [61,63) [63,67) [67,77] #> 36 27 31 33 35 36 26 19 34 26
## Example 2 age_3=equal_freq(var=heart_disease$age, n_bins = 5) summary(age_3)
#> [29,46) [46,54) [54,59) [59,63) [63,77] #> 63 64 71 45 60