Skip to contents

Create a new categorical variable based on a numeric variable.

Usage

var_cut(newvr, vr, breaks, labels)

Arguments

newvr

name of the new factor variable to be created

vr

numeric variable

breaks

see cut()

labels

see cut()

Value

Survey object

See also

Other variables: var_all(), var_any(), var_case(), var_collapse(), var_copy(), var_cross(), var_not()

Examples

set_survey(namcs2019sv)
#>                         Survey info {NAMCS 2019 PUF}                         
#> ┌───────────┬──────────────┬────────────────────────────────────────────────┐
#> │ Variables │ Observations │ Design                                         │
#> ├───────────┼──────────────┼────────────────────────────────────────────────┤
#> │        33 │        8,250 │ Stratified 1 - level Cluster Sampling design   │
#> │           │              │ (with replacement)                             │
#> │           │              │ With (398) clusters.                           │
#> │           │              │ namcs2019sv = survey::svydesign(ids = ~CPSUM,  │
#> │           │              │ strata = ~CSTRATM, weights = ~PATWT            │
#> │           │              │ , data = namcs2019sv_df)                       │
#> └───────────┴──────────────┴────────────────────────────────────────────────┘
#> 
# In some data systems, variables might contain "special values". For example,
# negative values might indicate unknowns (which should be coded as `NA`).
# Though in this particular data, there are no unknowns.
var_cut("Age group"
  , "AGE"
  , c(-Inf, -0.1, 0, 4, 14, 64, Inf)
  , c(NA, "Under 1", "1-4", "5-14", "15-64", "65 and over"))
tab("Age group")
#>                               Age group (knowns only) {NAMCS 2019 PUF}                              
#> ┌─────────────┬───────┬─────────────┬──────────┬──────────┬──────────┬─────────┬─────┬──────┬──────┐
#> │ Level       │     n │      Number │ SE (000) │ LL (000) │ UL (000) │ Percent │  SE │   LL │   UL │
#> │             │       │       (000) │          │          │          │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┤
#> │ Under 1     │   203 │      31,148 │    5,282 │   22,269 │   43,566 │     3   │ 0.5 │  2.1 │  4.1 │
#> ├─────────────┼───────┼─────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┤
#> │ 1-4         │   281 │      38,240 │    5,444 │   28,864 │   50,662 │     3.7 │ 0.5 │  2.7 │  4.8 │
#> ├─────────────┼───────┼─────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┤
#> │ 5-14        │   403 │      48,529 │    5,741 │   38,430 │   61,282 │     4.7 │ 0.5 │  3.7 │  5.9 │
#> ├─────────────┼───────┼─────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┤
#> │ 15-64       │ 4,260 │     544,632 │   36,082 │  478,254 │  620,223 │    52.5 │ 2   │ 48.6 │ 56.5 │
#> ├─────────────┼───────┼─────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┤
#> │ 65 and over │ 3,103 │     373,935 │   24,523 │  328,777 │  425,296 │    36.1 │ 1.9 │ 32.3 │ 40   │
#> └─────────────┴───────┴─────────────┴──────────┴──────────┴──────────┴─────────┴─────┴──────┴──────┘
#>   N = 8250. Checked NCHS presentation standards. Nothing to report.                                 
#>