Skip to contents

Calculate proportion statistics as in srv_prop_stats but for all combinations of grouping variables (grp_vars) and subject variables (sbj_vars). Similar to the results of srv_prop_stats_crossing but reformatted in a wide-custom format.

Usage

srv_prop_stats_crossing_wide(
  data,
  grp_vars,
  sbj_vars,
  na.rm = FALSE,
  mr_collapse = "Selected",
  comparison_format = NULL,
  wt = NULL
)

Arguments

data

a dataframe

grp_vars

<tidy-select> expression for grouping variables

sbj_vars

<tidy-select> expression for subject variables

na.rm

drop subject variable NA categories before calculating percentages? By default is FALSE

mr_collapse

Character. (default: "Selected") Level in the MR variable levels to be used to collapse similar variables.

comparison_format

Character. Specifies how comparison variables should be distinguished. When set to NULL (default), comparison don't occur.

wt

optional weight variable

Value

A dataframe of nested custom proportion stat.

Examples


library(dplyr)

data("ds_custom")

## No comparison:
# calculate and report a custom proportion statistics
res_1 <-
srv_prop_stats_crossing_wide(
  ds_custom,
  grp_vars = s_hhincome,
  sbj_vars = c(starts_with("m1_wrap_ai")),
  na.rm = TRUE,
  wt = weight
)
#> 
#> ── Detecting Data Type ──
#> 
#> • SR Variables: 3
#> • MR Variables: 0
#> ! Found 1 column with missing or invalid labels: "weight".
#> ℹ Replacing these labels with {.code NA_character_}
#> ! Found 2 columns with missing or invalid labels: "weight" and "group".
#> ℹ Replacing these labels with {.code NA_character_}

## MR variables should be recoded to Selected | Not Selected before
## applying the function on the data
res_2 <-
srv_prop_stats_crossing_wide(
  ds_custom,
  grp_vars = s_hhincome,
  sbj_vars = starts_with("d_social_use_ind_"),
  na.rm = TRUE,
  wt = weight
)
#> 
#> ── Detecting Data Type ──
#> 
#> • SR Variables: 0
#> • MR Variables: 1
#> ! Found 1 column with missing or invalid labels: "weight".
#> ℹ Replacing these labels with {.code NA_character_}
#> ! Found 2 columns with missing or invalid labels: "weight" and "group".
#> ℹ Replacing these labels with {.code NA_character_}

## Example to show how to create comparison ##

## Take a look at these variable names in `ds_custom`:
## m1_wrap_covid_ind, m1_wrap_covid_soc, m1_wrap_ai_ind, m1_wrap_ai_soc, etc
## Variables ending with _ind are asking questions with individuals in mind
## Variables ending with _soc are asking the same questions but with the society in mind
## We want to align these variables side by side for comparison.

res_3 <-
srv_prop_stats_crossing_wide(
  ds_custom,
  grp_vars = s_hhincome,
  sbj_vars = c(starts_with("m1_wrap_ai"), starts_with("m1_wrap_money")),
  na.rm = TRUE,
  wt = weight,
  comparison_format = c("Personal" = "_ind$", "Perceived" = "_soc$")
)
#> 
#> ── Detecting Data Type ──
#> 
#> • SR Variables: 6
#> • MR Variables: 0
#> ! Found 1 column with missing or invalid labels: "weight".
#> ℹ Replacing these labels with {.code NA_character_}
#> ! Found 2 columns with missing or invalid labels: "weight" and "group".
#> ℹ Replacing these labels with {.code NA_character_}
#> ! Some subject variables do not meet the comparison criteria.
#> • "m1_wrap_ai_com" and "m1_wrap_money_com" will be dropped.

res_4 <-
srv_prop_stats_crossing_wide(
  ds_custom,
  grp_vars = s_hhincome,
  sbj_vars =  c(starts_with("m1_wrap_ai"), starts_with("m1_wrap_hrdpst")),
  na.rm = TRUE,
  wt = weight,
  comparison_format = c("Personal" = "_ind$", "Perceived" = "_soc$", "Community" = "_com$")
)
#> 
#> ── Detecting Data Type ──
#> 
#> • SR Variables: 5
#> • MR Variables: 0
#> ! Found 1 column with missing or invalid labels: "weight".
#> ℹ Replacing these labels with {.code NA_character_}
#> ! Found 2 columns with missing or invalid labels: "weight" and "group".
#> ℹ Replacing these labels with {.code NA_character_}
#> ! Some subject variables do not meet the comparison criteria.
#> • "m1_wrap_hrdpst_open" will be dropped.
#> ! Some subject variables do not have complete cases.
#> • "m1_wrap_hrdpst" will be dropped.

res_5 <-
srv_prop_stats_crossing_wide(
  ds_custom,
  grp_vars = s_hhincome,
  sbj_vars = starts_with("d_social"),
  na.rm = TRUE,
  wt = weight,
  comparison_format = c("Personal" = "_ind$", "Community" = "_com$")
)
#> 
#> ── Detecting Data Type ──
#> 
#> • SR Variables: 0
#> • MR Variables: 2
#> ! Found 1 column with missing or invalid labels: "weight".
#> ℹ Replacing these labels with {.code NA_character_}
#> ! Found 2 columns with missing or invalid labels: "weight" and "group".
#> ℹ Replacing these labels with {.code NA_character_}