Skip to contents

Discretize a continuous variable into labeled intervals using lower and upper bounds. Use for binning variables in analysis or reporting pipelines.

Usage

cut_and_label(x, upper_bounds, prefix = "")

Arguments

x

numeric. Variable to cut into intervals.

upper_bounds

numeric. Upper bounds of intervals (inclusive).

prefix

character(1). Prefix for labels. Default "".

Value

factor. Labeled intervals for each value in x.

Details

  • Sorts and processes upper bounds, ensuring all values are classified.

  • Creates labels in the form prefix_lower_upper or prefix_lower_plus for Inf.

  • Returns a factor with labeled intervals.

  • Assumes numeric input and valid bounds; errors if bounds are invalid.

Settings

None.

Examples

## Not run:
x <- 1:10
cut_and_label(x, c(1, 2, 3, 4), "test")
#>  [1] test_0_1    test_2      test_3      test_4_plus test_4_plus test_4_plus
#>  [7] test_4_plus test_4_plus test_4_plus test_4_plus
#> Levels: test_0_1 test_2 test_3 test_4_plus
num_kids <- c(0, 1, 2, 3)
cut_and_label(num_kids, c(0, 1), "num_kids")
#> [1] num_kids_0      num_kids_1_plus num_kids_1_plus num_kids_1_plus
#> Levels: num_kids_0 num_kids_1_plus
age <- c(4, 12, 17, 34, 64, 65)
cut_and_label(age, c(4, 17, 34, 64), "age")
#> [1] age_0_4     age_5_17    age_5_17    age_18_34   age_35_64   age_65_plus
#> Levels: age_0_4 age_5_17 age_18_34 age_35_64 age_65_plus
income <- c(15000, 22000, 25000, 30000, 40000, 60000, 300000)
cut_and_label(income, c(19999, 29999, 39999, 49999), "income")
#> [1] income_0_19999     income_20000_29999 income_20000_29999 income_30000_39999
#> [5] income_40000_49999 income_50000_plus  income_50000_plus 
#> 5 Levels: income_0_19999 income_20000_29999 ... income_50000_plus
## End(Not run)