R/e_table_sum_freq_prop.R
e_table_sum_freq_prop.Rd
Create a frequency/proportion summary table by a set of variables
e_table_sum_freq_prop(
dat,
var_names,
sw_sort_prop = TRUE,
sw_drop_NA = FALSE,
sw_totals = FALSE
)
data.frame or tibble
list of variable names
TRUE/FALSE to sort the last variable descending by proportion within the other variables
TRUE/FALSE to drop NAs in var_names
variables before calculating proportions
TRUE/FALSE to include totals
tab_summary a summary table
# Create data with missing values for examples
dat_miss = dat_mtcars_e
prop_missing = 0.10
n_missing = sample.int(n = prod(dim(dat_miss)), size = round( prop_missing * prod(dim(dat_miss))))
ind_missing = expand.grid(1:dim(dat_miss)[1], 1:dim(dat_miss)[2])[n_missing, ]
for (i_row in seq_along(n_missing)) {
dat_miss[ind_missing[i_row,1], ind_missing[i_row,2] ] <- NA
}
# do not sort, with NAs, no totals
e_table_sum_freq_prop(
dat = dat_miss
, var_names = c("vs", "am", "cyl")
, sw_sort_prop = FALSE
, sw_drop_NA = FALSE
, sw_totals = FALSE
) %>%
print(n = Inf)
#> # A tibble: 15 × 5
#> vs am cyl n prop
#> <fct> <fct> <fct> <int> <dbl>
#> 1 V-shaped automatic eight 9 0.9
#> 2 V-shaped automatic NA 1 0.1
#> 3 V-shaped manual six 1 1
#> 4 V-shaped NA six 1 0.333
#> 5 V-shaped NA eight 2 0.667
#> 6 straight automatic four 3 0.429
#> 7 straight automatic six 4 0.571
#> 8 straight manual four 4 0.8
#> 9 straight manual NA 1 0.2
#> 10 straight NA four 1 0.5
#> 11 straight NA NA 1 0.5
#> 12 NA automatic eight 1 1
#> 13 NA NA four 1 0.333
#> 14 NA NA six 1 0.333
#> 15 NA NA eight 1 0.333
# sorted by proportion, with NAs, no totals
e_table_sum_freq_prop(
dat = dat_miss
, var_names = c("vs", "am", "cyl")
, sw_sort_prop = TRUE
, sw_drop_NA = FALSE
, sw_totals = FALSE
) %>%
print(n = Inf)
#> # A tibble: 15 × 5
#> vs am cyl n prop
#> <fct> <fct> <fct> <int> <dbl>
#> 1 V-shaped automatic eight 9 0.9
#> 2 V-shaped automatic NA 1 0.1
#> 3 V-shaped manual six 1 1
#> 4 V-shaped NA eight 2 0.667
#> 5 V-shaped NA six 1 0.333
#> 6 straight automatic six 4 0.571
#> 7 straight automatic four 3 0.429
#> 8 straight manual four 4 0.8
#> 9 straight manual NA 1 0.2
#> 10 straight NA four 1 0.5
#> 11 straight NA NA 1 0.5
#> 12 NA automatic eight 1 1
#> 13 NA NA four 1 0.333
#> 14 NA NA six 1 0.333
#> 15 NA NA eight 1 0.333
# sorted by proportion, no NAs, no totals
e_table_sum_freq_prop(
dat = dat_miss
, var_names = c("vs", "am", "cyl")
, sw_sort_prop = TRUE
, sw_drop_NA = TRUE
, sw_totals = FALSE
) %>%
print(n = Inf)
#> # A tibble: 5 × 5
#> vs am cyl n prop
#> <fct> <fct> <fct> <int> <dbl>
#> 1 V-shaped automatic eight 9 1
#> 2 V-shaped manual six 1 1
#> 3 straight automatic six 4 0.571
#> 4 straight automatic four 3 0.429
#> 5 straight manual four 4 1
# sorted by proportion, no NAs, with totals
e_table_sum_freq_prop(
dat = dat_miss
, var_names = c("vs", "am", "cyl")
, sw_sort_prop = TRUE
, sw_drop_NA = TRUE
, sw_totals = TRUE
) %>%
print(n = Inf)
#> # A tibble: 12 × 5
#> vs am cyl n prop
#> <chr> <chr> <chr> <int> <dbl>
#> 1 V-shaped automatic eight 9 1
#> 2 V-shaped manual six 1 1
#> 3 straight automatic six 4 0.571
#> 4 straight automatic four 3 0.429
#> 5 straight manual four 4 1
#> 6 V-shaped automatic _TOTAL_ 9 0.9
#> 7 V-shaped manual _TOTAL_ 1 0.1
#> 8 straight automatic _TOTAL_ 7 0.636
#> 9 straight manual _TOTAL_ 4 0.364
#> 10 straight _TOTAL_ _TOTAL_ 11 0.524
#> 11 V-shaped _TOTAL_ _TOTAL_ 10 0.476
#> 12 _TOTAL_ _TOTAL_ _TOTAL_ 21 1