Skip to contents

Collapse two or more levels of a factor variable into a single level.

Usage

var_collapse(vr, newlevel, oldlevels)

Arguments

vr

factor variable

newlevel

name of the new level

oldlevels

vector of old levels

Value

Survey object

See also

Other variables: var_all(), var_any(), var_case(), var_copy(), var_cross(), var_cut(), 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)                       │
#> └───────────┴──────────────┴────────────────────────────────────────────────┘
#> 
tab("PRIMCARE")
#>               Are you the patient's primary care provider? (knowns only) {NAMCS 2019 PUF}              
#> ┌─────────┬───────┬────────────┬──────────┬──────────┬──────────┬─────────┬─────┬──────┬──────┬───────┐
#> │ Level   │     n │     Number │ SE (000) │ LL (000) │ UL (000) │ Percent │  SE │   LL │   UL │ Flags │
#> │         │       │      (000) │          │          │          │         │     │      │      │       │
#> ├─────────┼───────┼────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┼───────┤
#> │ Blank   │    16 │      1,150 │      478 │      440 │    3,005 │     0.1 │ 0   │  0   │  0.2 │ Cx    │
#> ├─────────┼───────┼────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┼───────┤
#> │ Unknown │   300 │     39,519 │    9,507 │   24,520 │   63,692 │     3.8 │ 0.9 │  2.3 │  6   │       │
#> ├─────────┼───────┼────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┼───────┤
#> │ Yes     │ 2,278 │    383,481 │   28,555 │  331,362 │  443,798 │    37   │ 2.6 │ 31.9 │ 42.3 │       │
#> ├─────────┼───────┼────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┼───────┤
#> │ No      │ 5,656 │    612,335 │   43,282 │  533,050 │  703,413 │    59.1 │ 2.5 │ 53.9 │ 64.1 │       │
#> └─────────┴───────┴────────────┴──────────┴──────────┴──────────┴─────────┴─────┴──────┴──────┴───────┘
#>   N = 8250. Checked NCHS presentation standards: Cx: suppress count                                    
#>   (and rate).                                                                                          
#> 
var_collapse("PRIMCARE", "Unknown if PCP", c("Blank", "Unknown"))
tab("PRIMCARE")
#>             Are you the patient's primary care provider? (knowns only) {NAMCS 2019 PUF}             
#> ┌─────────────┬───────┬─────────────┬──────────┬──────────┬──────────┬─────────┬─────┬──────┬──────┐
#> │ Level       │     n │      Number │ SE (000) │ LL (000) │ UL (000) │ Percent │  SE │   LL │   UL │
#> │             │       │       (000) │          │          │          │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┤
#> │ Unknown if  │   316 │      40,669 │    9,479 │   25,619 │   64,560 │     3.9 │ 0.9 │  2.4 │  6.1 │
#> │ PCP         │       │             │          │          │          │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┤
#> │ Yes         │ 2,278 │     383,481 │   28,555 │  331,362 │  443,798 │    37   │ 2.6 │ 31.9 │ 42.3 │
#> ├─────────────┼───────┼─────────────┼──────────┼──────────┼──────────┼─────────┼─────┼──────┼──────┤
#> │ No          │ 5,656 │     612,335 │   43,282 │  533,050 │  703,413 │    59.1 │ 2.5 │ 53.9 │ 64.1 │
#> └─────────────┴───────┴─────────────┴──────────┴──────────┴──────────┴─────────┴─────┴──────┴──────┘
#>   N = 8250. Checked NCHS presentation standards. Nothing to report.                                 
#>