View source: R/collapse_n_wt.R
collapse_n_wt | R Documentation |
This is just a quick wrapper for a common, tedious task of collapsing several demographic groups, such as income brackets, into larger groups and taking a weighted mean based on a set of survey weights.
collapse_n_wt(
data,
...,
.lvls,
.group = group,
.value = value,
.weight = weight,
.fill_wts = FALSE,
.digits = NULL
)
data |
A data frame, such as returned by |
... |
Bare column names to use for grouping, including the |
.lvls |
A named list, where values are character vectors of smaller
groups (e.g. |
.group |
Bare column name of where groups should be found. Default: group |
.value |
Bare column name of where values should be found. Default: value |
.weight |
Bare column name of where group weights should be found. Default: weight |
.fill_wts |
Logical: if |
.digits |
Numeric: if given, weighted means will be rounded to this
number of digits. If |
A data frame with summarized values. The .group
column will have
the collapsed groups, and the .value
column will have average values.
xtab2df()
, read_weights()
, forcats::fct_collapse()
# collapse income groups, such that <$15K, $15K-$30K become <$30K, etc
income_lvls <- list(
"<$30K" = c("<$15K", "$15K-$30K"),
"$30K-$100K" = c("$30K-$50K", "$50K-$75K", "$75K-$100K"),
"$100K+" = c("$100K-$200K", "$200K+")
)
cws_demo |>
dplyr::filter(category %in% c("Greater New Haven", "Income")) |>
collapse_n_wt(code:response, .lvls = income_lvls, .digits = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.