w_modus() finds the mode (most frequently occurring value) of your data
using survey weights for population-representative results. The mode tells you
which category or value is the most common in your population. This is
especially useful for categorical variables (e.g., the most common education
level, the most frequent employment status).
Unlike mean and median, the mode works with both numeric and categorical data.
Arguments
- data
Your survey data (a data frame or tibble)
- ...
The variables you want to analyze. Works best with categorical or discrete numeric variables. You can list multiple variables or use helpers like
starts_with("trust")- weights
Survey weights to make results representative of your population. Without weights, the mode is simply the most frequent value in your sample. Give a column name (unquoted or as a string), an expression such as
sampling_weight * 2, or a numeric vector with one weight per row.- na.rm
Remove missing values before calculating? (Default: TRUE). With
FALSE, the result for a variable that contains missing values isNA(as in base R).
Value
Population-weighted mode(s) with sample size information, including the most common value (by weighted frequency), effective sample size (effective N), and the number of valid observations used.
Details
Understanding the Results
Weighted Mode: The value that occurs most frequently in the weighted population. For weighted data, the mode is the value whose observations have the largest total weight.
Effective N: How many independent observations your weighted data represents.
N / Missing: Valid and missing cases. With weights, both are sums of weights (displayed rounded), as SPSS reports them under
WEIGHT BY; Kish's effective N is shown bysummary().
If multiple values share the highest (weighted) frequency, the smallest
value (for factors: the first level) is returned, as in SPSS; the print
flags such results ("Multiple modes exist") and $results$n_modes
holds the number of tied values.
See also
w_median for the weighted middle value.
w_mean for weighted means.
frequency for complete frequency tables of categorical variables.
describe for comprehensive descriptive statistics including the mode.
Other weighted_statistics:
w_iqr(),
w_kurtosis(),
w_mean(),
w_median(),
w_quantile(),
w_range(),
w_sd(),
w_se(),
w_skew(),
w_var()
Examples
# Load required packages and data
library(dplyr)
data(survey_data)
# Basic weighted mode (most frequent value)
survey_data %>% w_modus(gender, weights = sampling_weight)
#>
#> Weighted Mode Statistics
#> ------------------------
#> Weights: sampling_weight
#>
#> -------------------------------
#> Variable Mode N Missing
#> -------------------------------
#> gender Female 2516 0
#> -------------------------------
# Multiple variables (works best with categorical/discrete data)
survey_data %>% w_modus(gender, region, weights = sampling_weight)
#>
#> Weighted Mode Statistics
#> ------------------------
#> Weights: sampling_weight
#>
#> -------------------------------
#> Variable Mode N Missing
#> -------------------------------
#> gender Female 2516 0
#> region West 2516 0
#> -------------------------------
# Grouped data
survey_data %>% group_by(region) %>% w_modus(gender, weights = sampling_weight)
#>
#> Weighted Mode Statistics
#> ------------------------
#> Weights: sampling_weight
#>
#> Group: region = East
#> --------------------
#>
#> ------------------------------
#> Variable Mode N Missing
#> ------------------------------
#> gender Female 509 0
#> ------------------------------
#>
#> Group: region = West
#> --------------------
#>
#> -------------------------------
#> Variable Mode N Missing
#> -------------------------------
#> gender Female 2007 0
#> -------------------------------
# In summarise context
survey_data %>% summarise(mode_gender = w_modus(gender, weights = sampling_weight))
#> # A tibble: 1 × 1
#> mode_gender
#> <fct>
#> 1 Female
# Unweighted (for comparison)
survey_data %>% w_modus(gender)
#>
#> Mode Statistics
#> ---------------
#>
#> -------------------------------
#> Variable Mode N Missing
#> -------------------------------
#> gender Female 2500 0
#> -------------------------------
