table_weightedpct: Calculate table of percentages or N of response distribution...

View source: R/table_weightedpct.R

table_weightedpctR Documentation

Calculate table of percentages or N of response distribution for survey items, survey weighted, disaggregated

Description

Calculate table of percentages or N of response distribution for survey items, survey weighted, disaggregated

Usage

table_weightedpct(
  df,
  vars_ids,
  vars_strata,
  vars_weights,
  formula_vars,
  ...,
  formula_vars_levels = 0:1,
  by_vars = NULL,
  pct = TRUE,
  willfilter = NULL,
  add_totals = FALSE,
  spread_key = NULL,
  spread_value = "prop",
  arrange_vars = NULL,
  include_SE = FALSE
)

Arguments

df

a data frame of individual survey data, where each row is an individual

vars_ids

a character vector of cluster ids, passed to srvyr::as_survey_design()

vars_strata

a character vector of strata ids, passed to srvyr::as_survey_design()

vars_weights

a character vector of survey weight ids, passed to srvyr::as_survey_design()

formula_vars

a character vector of variables to calculate the percentages of each level for

...

captures expressions to pass to dplyr::filter() or dplyr::transmute(), depending on the value of argument willfilter. See Details.

formula_vars_levels

a vector of the levels of the the formula_vars

by_vars

a character vector of variables to disaggregate results by. Default is NULL for no disaggregation. The columns listed must not include NAs.

pct

a logical variable indicating whether or not to calculate weighted percentages. Default is TRUE for weighted percentages. Set to FALSE for weighted N.

willfilter

a logical variable that tells the function whether or not to filter or transmute the data. Leave as default NULL to not filter or transmute. Set as TRUE to filter and FALSE to transmute. See Details.

add_totals

logical determining whether to create total rows or columns (as appropriate) that demonstrate the margin that sums to 100. Default is FALSE.

spread_key

a string with variable name to pass to names_from argument of tidyr::pivot_wider(). Default is NULL.

spread_value

a string with variable name to pass to values_from argument of tidyr::pivot_wider(). Default is "prop" (the column of percentages created within the function)

arrange_vars

a character vector with variables to pass to dplyr::arrange(). Default is NULL.

include_SE

a logical variable indicating whether to include the standard errors in the table. Default is FALSE. Currently does not work when adding totals, spreading or transmuting.

Details

If willfilter is NULL, the table is not filtered or transmuted. If willfilter is TRUE, the table is filtered before it is spread or arranged. If willfilter is FALSE, the table is transmuted after the spread and/or arrange. "..." captures the non-standard evaluation expressions (NSE) to pass to dplyr::filter or dplyr::transmute().

The function performs the following actions with the table after results are calculated in the following order (if applicable): filter, add totals, spread, arrange, transmute

Value

a tibble of weighted response percentages or N's

See Also

See vignette("programming", package = "dplyr") for more about non-standard evaluation (NSE)

Other table functions: table_basicstats(), table_unweightedpctn()

Examples

table_weightedpct(df_adults, 
    vars_ids = c("HHID", "PSU"),
    vars_strata = "strata",
    vars_weights = "weight",
    formula_vars = paste0("EF",1:10),
    formula_vars_levels = 1:5,
    by_vars = "sex")

lindsayevanslee/whomds documentation built on Sept. 9, 2023, 10:54 p.m.