Description Usage Arguments Details Value Examples
Takes a categorical variable and returns the weighted or unweighted percentage or total of each category. Can include a grouping variable. Can be used to compare weights versus one another.
1 2 3 4 5 6 7 8 9 10 11 12 |
var |
|
df |
The |
wt |
A |
by |
For creating crosstabulations, an optional |
by_total |
|
percent |
Should the results be scaled as percentages? Defaults to |
include_unw |
Include unweighted frequencies in the output along with weighted.
Defaults to |
digits |
The number of decimal points displayed. Defaults to value specified
in |
complete |
TRUE/FALSE: Should factor levels with no
observations be included in the results? Defaults to |
na.rm |
If |
If no arguments are supplied to by
, then the column names
will be the weight names. If arguments are supplied to by
, then the
column names will be the categories of the grouping variable, and the
output will have an additional column for the weight name.
A data.frame
with a column for the variable name, columns displaying
percentages or totals, and additional columns as specified by arguments to
this function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | library(dplyr)
# Basic unweighted crosstab
get_totals("q1", dec13_excerpt)
# Totals instead of percentages
get_totals("q1", dec13_excerpt, percent = FALSE)
# Weighted crosstab
get_totals("receduc", dec13_excerpt, wt = "weight")
# Weighted crosstab by grouping variable
get_totals("q1", dec13_excerpt, wt = "weight", by = "receduc")
# Compare weights, including unweighted
# Let's make a fake weight by combining the landline and cellphone weights
dec13_excerpt <- dec13_excerpt %>% mutate(fake_weight = coalesce(llweight, cellweight))
get_totals("q1", dec13_excerpt, wt = c("weight", "fake_weight"), include_unw = TRUE)
# Use dplyr::filter along with complete = FALSE to remove unwanted categories from the base
get_totals("q1", dec13_excerpt %>% filter(q1 != "Don't know/Refused (VOL.)"), wt = "weight",
complete = FALSE)
# Alternatively, filter unwanted categories out beforehand with dk_to_na
# and then use na.rm = TRUE
dec13_excerpt <- dec13_excerpt %>% mutate(q1 = dk_to_na(q1))
get_totals("q1", dec13_excerpt, wt = "weight", na.rm = TRUE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.