Cut variable into labeled intervals
cut_and_label.RdDiscretize a continuous variable into labeled intervals using lower and upper bounds. Use for binning variables in analysis or reporting pipelines.
Details
Sorts and processes upper bounds, ensuring all values are classified.
Creates labels in the form
prefix_lower_upperorprefix_lower_plusfor Inf.Returns a factor with labeled intervals.
Assumes numeric input and valid bounds; errors if bounds are invalid.
See also
scripts/utilities/cut_label.R
Other io utilities:
clean_data_dict(),
db_table_has_geometry(),
fetch_acs(),
fetch_all_hts_tables(),
fetch_from_db(),
fetch_hts_table(),
fetch_pums(),
fetch_study_region(),
find_project_root(),
fix_value_labels_on_load(),
get_db_table_name(),
print_params(),
pums_checksum(),
read_from_db(),
read_pums_codebook(),
record_checksum(),
sampled_latlon_to_bg()
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)