Create subsets of the survey using one variable, and tabulate another variable within each of the subsets. Interact two variables and tabulate.
Arguments
- vr
 variable to tabulate
- vrby
 use this variable to subset the survey
- max_levels
 a categorical variable can have at most this many levels. Used to avoid printing huge tables.
- lvls
 (optional) only show these levels of
vrby- test
 if
TRUE, performs a test of association and t-tests for all pairs of levels ofvrandvrby. Iftestis the name of a level ofvr, performs a conditional independence test for that level.- alpha
 significance level for tests
- p_adjust
 adjust p-values for multiple comparisons?
- drop_na
 drop missing values (
NA)? Categorical variables only.
Details
tab_subset() creates subsets using the levels of vrby, and tabulates
vr in each subset. Optionally, only use the lvls levels of vrby.
vr can be categorical (factor or character), logical, or numeric.
tab_cross() crosses or interacts vr and vrby and tabulates the new
variable. Tables created using tab_subset() and tab_cross() have the same
counts but different percentages. With tab_subset(), percentages within each
subset add up to 100%. With tab_cross(), percentages across the entire
population add up to 100%. Also see var_cross().
test = TRUE performs a test of association between the two variables. Also
performs t-tests for all pairs of levels of vr and vrby.
test = "{LEVEL}", where {LEVEL} is a level of vr, performs a
conditional independence test to compare the proportion of
vr = "{LEVEL}" for different values of vrby.
See also
Other tables:
tab(),
tab_rate(),
tab_subset_rate(),
total(),
total_rate()
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)                       │
#> └───────────┴──────────────┴────────────────────────────────────────────────┘
#> 
# For each SEX, tabulate AGER
tab_subset("AGER", "SEX")
#>                          Patient age recode (Patient sex = Female) {NAMCS 2019 PUF}                         
#> ┌─────────────┬───────┬─────────────┬────────────┬─────────────┬─────────────┬─────────┬─────┬──────┬──────┐
#> │ Level       │     n │      Number │         SE │          LL │          UL │ Percent │  SE │   LL │   UL │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ Under 15    │   434 │  59,957,823 │  7,205,594 │  47,318,228 │  75,973,693 │     9.9 │ 1.2 │  7.6 │ 12.6 │
#> │ years       │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 15-24 years │   346 │  41,128,003 │  4,532,466 │  33,065,609 │  51,156,253 │     6.8 │ 0.7 │  5.4 │  8.4 │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 25-44 years │   923 │ 113,708,461 │ 11,461,189 │  93,256,445 │ 138,645,797 │    18.8 │ 1.6 │ 15.8 │ 22.1 │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 45-64 years │ 1,253 │ 175,978,133 │ 16,008,541 │ 147,152,826 │ 210,449,940 │    29.1 │ 1.7 │ 25.7 │ 32.6 │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 65-74 years │   891 │ 120,099,493 │ 11,066,146 │ 100,171,315 │ 143,992,203 │    19.8 │ 1.5 │ 17   │ 22.9 │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 75 years    │   762 │  94,173,155 │ 11,085,372 │  74,682,310 │ 118,750,789 │    15.6 │ 1.5 │ 12.8 │ 18.7 │
#> │ and over    │       │             │            │             │             │         │     │      │      │
#> └─────────────┴───────┴─────────────┴────────────┴─────────────┴─────────────┴─────────┴─────┴──────┴──────┘
#>   N = 4609.                                                                                                 
#> 
#>                           Patient age recode (Patient sex = Male) {NAMCS 2019 PUF}                          
#> ┌─────────────┬───────┬─────────────┬────────────┬─────────────┬─────────────┬─────────┬─────┬──────┬──────┐
#> │ Level       │     n │      Number │         SE │          LL │          UL │ Percent │  SE │   LL │   UL │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ Under 15    │   453 │  57,958,950 │  7,727,594 │  44,569,688 │  75,370,504 │    13.4 │ 1.7 │ 10.3 │ 17.1 │
#> │ years       │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 15-24 years │   196 │  23,727,695 │  4,343,932 │  16,457,071 │  34,210,431 │     5.5 │ 0.8 │  4   │  7.4 │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 25-44 years │   512 │  56,562,143 │  7,276,983 │  43,860,836 │  72,941,520 │    13.1 │ 1.3 │ 10.7 │ 15.8 │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 45-64 years │ 1,030 │ 133,527,822 │ 12,956,239 │ 110,319,199 │ 161,619,006 │    30.9 │ 1.6 │ 27.8 │ 34.3 │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 65-74 years │   770 │  86,766,489 │  6,766,876 │  74,409,284 │ 101,175,865 │    20.1 │ 1.5 │ 17.3 │ 23.1 │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 75 years    │   680 │  72,896,189 │  6,660,855 │  60,871,965 │  87,295,593 │    16.9 │ 1.5 │ 14   │ 20.2 │
#> │ and over    │       │             │            │             │             │         │     │      │      │
#> └─────────────┴───────┴─────────────┴────────────┴─────────────┴─────────────┴─────────┴─────┴──────┴──────┘
#>   N = 3641.                                                                                                 
#> 
# Same counts as tab_subset(), but different percentages.
tab_cross("AGER", "SEX")
#>                            (Patient age recode) x (Patient sex) {NAMCS 2019 PUF}                            
#> ┌─────────────┬───────┬─────────────┬────────────┬─────────────┬─────────────┬─────────┬─────┬──────┬──────┐
#> │ Level       │     n │      Number │         SE │          LL │          UL │ Percent │  SE │   LL │   UL │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ Under 15    │   434 │  59,957,823 │  7,205,594 │  47,318,228 │  75,973,693 │     5.8 │ 0.7 │  4.5 │  7.3 │
#> │ years:      │       │             │            │             │             │         │     │      │      │
#> │ Female      │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 15-24       │   346 │  41,128,003 │  4,532,466 │  33,065,609 │  51,156,253 │     4   │ 0.4 │  3.2 │  4.9 │
#> │ years:      │       │             │            │             │             │         │     │      │      │
#> │ Female      │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 25-44       │   923 │ 113,708,461 │ 11,461,189 │  93,256,445 │ 138,645,797 │    11   │ 1   │  9   │ 13.2 │
#> │ years:      │       │             │            │             │             │         │     │      │      │
#> │ Female      │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 45-64       │ 1,253 │ 175,978,133 │ 16,008,541 │ 147,152,826 │ 210,449,940 │    17   │ 1.1 │ 14.8 │ 19.3 │
#> │ years:      │       │             │            │             │             │         │     │      │      │
#> │ Female      │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 65-74       │   891 │ 120,099,493 │ 11,066,146 │ 100,171,315 │ 143,992,203 │    11.6 │ 1   │  9.7 │ 13.7 │
#> │ years:      │       │             │            │             │             │         │     │      │      │
#> │ Female      │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 75 years    │   762 │  94,173,155 │ 11,085,372 │  74,682,310 │ 118,750,789 │     9.1 │ 0.9 │  7.3 │ 11.1 │
#> │ and over:   │       │             │            │             │             │         │     │      │      │
#> │ Female      │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ Under 15    │   453 │  57,958,950 │  7,727,594 │  44,569,688 │  75,370,504 │     5.6 │ 0.7 │  4.3 │  7.2 │
#> │ years: Male │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 15-24       │   196 │  23,727,695 │  4,343,932 │  16,457,071 │  34,210,431 │     2.3 │ 0.4 │  1.6 │  3.2 │
#> │ years: Male │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 25-44       │   512 │  56,562,143 │  7,276,983 │  43,860,836 │  72,941,520 │     5.5 │ 0.6 │  4.3 │  6.8 │
#> │ years: Male │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 45-64       │ 1,030 │ 133,527,822 │ 12,956,239 │ 110,319,199 │ 161,619,006 │    12.9 │ 1   │ 10.9 │ 15.1 │
#> │ years: Male │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 65-74       │   770 │  86,766,489 │  6,766,876 │  74,409,284 │ 101,175,865 │     8.4 │ 0.6 │  7.2 │  9.7 │
#> │ years: Male │       │             │            │             │             │         │     │      │      │
#> ├─────────────┼───────┼─────────────┼────────────┼─────────────┼─────────────┼─────────┼─────┼──────┼──────┤
#> │ 75 years    │   680 │  72,896,189 │  6,660,855 │  60,871,965 │  87,295,593 │     7   │ 0.6 │  5.9 │  8.3 │
#> │ and over:   │       │             │            │             │             │         │     │      │      │
#> │ Male        │       │             │            │             │             │         │     │      │      │
#> └─────────────┴───────┴─────────────┴────────────┴─────────────┴─────────────┴─────────┴─────┴──────┴──────┘
#>   N = 8250.                                                                                                 
#> 
# Numeric variables
tab_subset("NUMMED", "AGER")
#> Number of medications coded (for different levels of Patient age recode) {NAMCS 2019 PUF}
#> ┌───────────────────┬─────────┬──────┬───────┬──────┬──────┬──────┐
#> │ Level             │ % known │ Mean │   SEM │   LL │   UL │   SD │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ Under 15 years    │     100 │ 1.58 │ 0.168 │ 1.25 │ 1.91 │ 1.75 │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ 15-24 years       │     100 │ 1.64 │ 0.112 │ 1.42 │ 1.86 │ 1.7  │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ 25-44 years       │     100 │ 2.15 │ 0.225 │ 1.71 │ 2.59 │ 2.74 │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ 45-64 years       │     100 │ 3.49 │ 0.303 │ 2.9  │ 4.09 │ 4.49 │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ 65-74 years       │     100 │ 4.44 │ 0.431 │ 3.6  │ 5.29 │ 5.03 │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ 75 years and over │     100 │ 5.53 │ 0.494 │ 4.56 │ 6.5  │ 5.59 │
#> └───────────────────┴─────────┴──────┴───────┴──────┴──────┴──────┘
#> 
# Hypothesis testing
tab_subset("NUMMED", "AGER", test = TRUE)
#> Number of medications coded (for different levels of Patient age recode) {NAMCS 2019 PUF}
#> ┌───────────────────┬─────────┬──────┬───────┬──────┬──────┬──────┐
#> │ Level             │ % known │ Mean │   SEM │   LL │   UL │   SD │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ Under 15 years    │     100 │ 1.58 │ 0.168 │ 1.25 │ 1.91 │ 1.75 │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ 15-24 years       │     100 │ 1.64 │ 0.112 │ 1.42 │ 1.86 │ 1.7  │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ 25-44 years       │     100 │ 2.15 │ 0.225 │ 1.71 │ 2.59 │ 2.74 │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ 45-64 years       │     100 │ 3.49 │ 0.303 │ 2.9  │ 4.09 │ 4.49 │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ 65-74 years       │     100 │ 4.44 │ 0.431 │ 3.6  │ 5.29 │ 5.03 │
#> ├───────────────────┼─────────┼──────┼───────┼──────┼──────┼──────┤
#> │ 75 years and over │     100 │ 5.53 │ 0.494 │ 4.56 │ 6.5  │ 5.59 │
#> └───────────────────┴─────────┴──────┴───────┴──────┴──────┴──────┘
#> 
#> Association between Number of medications coded and Patient age recode {NAMCS 2019 PUF}
#> ┌────────────────┬──────────────────────┬──────────────────────┬─────────┬──────┐
#> │ Test statistic │ Degrees of freedom 1 │ Degrees of freedom 2 │ p-value │ Flag │
#> ├────────────────┼──────────────────────┼──────────────────────┼─────────┼──────┤
#> │             13 │                    5 │                  334 │       0 │ *    │
#> └────────────────┴──────────────────────┴──────────────────────┴─────────┴──────┘
#>   Wald test. *: p <= 0.05                                                        
#> 
#> Comparison of Number of medications coded across all possible pairs of Patient age recode {NAMCS 2019 PUF}
#> ┌────────────────┬───────────────────┬────────────────┬────────────────────┬─────────┬──────┐
#> │ Level 1        │ Level 2           │ Test statistic │ Degrees of freedom │ p-value │ Flag │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ Under 15 years │ 15-24 years       │           0.33 │                173 │   0.739 │      │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ Under 15 years │ 25-44 years       │           2.03 │                267 │   0.043 │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ Under 15 years │ 45-64 years       │           5.59 │                315 │   0     │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ Under 15 years │ 65-74 years       │           6.2  │                294 │   0     │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ Under 15 years │ 75 years and over │           7.6  │                269 │   0     │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ 15-24 years    │ 25-44 years       │           2.19 │                268 │   0.029 │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ 15-24 years    │ 45-64 years       │           5.84 │                317 │   0     │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ 15-24 years    │ 65-74 years       │           6.35 │                310 │   0     │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ 15-24 years    │ 75 years and over │           7.56 │                296 │   0     │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ 25-44 years    │ 45-64 years       │           5.65 │                297 │   0     │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ 25-44 years    │ 65-74 years       │           5.53 │                296 │   0     │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ 25-44 years    │ 75 years and over │           7    │                290 │   0     │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ 45-64 years    │ 65-74 years       │           2.71 │                297 │   0.007 │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ 45-64 years    │ 75 years and over │           5.05 │                291 │   0     │ *    │
#> ├────────────────┼───────────────────┼────────────────┼────────────────────┼─────────┼──────┤
#> │ 65-74 years    │ 75 years and over │           3.05 │                280 │   0.002 │ *    │
#> └────────────────┴───────────────────┴────────────────┴────────────────────┴─────────┴──────┘
#>   Design-based t-test. *: p <= 0.05                                                          
#>