Proportion Statistics for all Combinations of Grouping and Subject Variables
Source:R/srv_prop_stats_crossing.R
srv_prop_stats_crossing.RdCalculate proportion statistics as in srv_prop_stats but for
all combinations of groping variables (grp_vars) and subject variables
(sbj_vars)
Arguments
- data
a dataframe
- grp_vars
<
tidy-select> expression for grouping variables- sbj_vars
<
tidy-select> expression for subject variables- wt
optional weight variable
- na.rm
drop subject variable NA categories before calculating percentages? By default is
FALSE- nested
return results in a nested form? Default is
TRUE
Examples
library(dplyr)
data("ds_newsletter")
# for each grouping variable: s_race and Region
# for each subject variable s_age, s_gender, s_hhincome
# calculate proportion statistics
res <-
ds_newsletter %>%
srv_prop_stats_crossing(c(s_race, Region), s_age:s_hhincome)
#> ! Found 1 column with missing or invalid labels: "Region".
#> ℹ Replacing these labels with {.code NA_character_}
# leave only combinations where at least one proportion is significantly
# different
res %>%
rowwise() %>%
mutate(min_pval = min(stats$pval)) %>%
ungroup() %>%
filter(min_pval < 0.05)
#> # A tibble: 2 × 6
#> grp_var grp_var_label sbj_var sbj_var_label stats min_pval
#> <chr> <chr> <chr> <chr> <list> <dbl>
#> 1 s_race Which racial or ethnic group … s_age What is your… <tibble> 0.00614
#> 2 s_race Which racial or ethnic group … s_hhin… What is your… <tibble> 0.00598