w_median() finds the median (middle value) of your data using survey
weights. The weighted median is the value where half the population falls below
and half falls above. Unlike the mean, the median is not pulled by extreme
values, making it a robust measure of the "typical" value in your population.
Arguments
- data
Your survey data (a data frame or tibble)
- ...
The numeric variables you want to analyze. You can list multiple variables or use helpers like
starts_with("income")- weights
Survey weights to make results representative of your population. Without weights, you get the simple sample median. 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 median(s) with sample size information, including the weighted median, effective sample size (effective N), and the number of valid observations used.
Details
Understanding the Results
Weighted Median: The value that splits the weighted population in half. 50% of the population (by weight) falls below this value, 50% above.
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().
Comparing the weighted median to the weighted mean is informative:
If they are similar, the distribution is roughly symmetric.
If the mean is much larger than the median, the distribution is right-skewed (a few very high values pull the mean up, e.g., income).
When to Use This
Use w_median() when:
Your data has outliers or is skewed (e.g., income, housing prices)
You want a robust "typical value" not influenced by extremes
You need the weighted 50th percentile
You need SPSS-compatible weighted median values
Formula
The weighted median is the weighted 50th percentile, computed with the
SPSS HAVERAGE (quantile Type 6) position h = 0.5 * (W + 1) on the
cumulative weights, with linear interpolation between bracketing
observations. It is identical to w_quantile(x, probs = 0.5) by
construction. Unweighted and weighted values reproduce SPSS FREQUENCIES
(with and without WEIGHT BY).
See also
median for the base R median function.
w_mean for weighted means.
w_quantile for arbitrary weighted percentiles.
describe for comprehensive descriptive statistics including the median.
Other weighted_statistics:
w_iqr(),
w_kurtosis(),
w_mean(),
w_modus(),
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 median
survey_data %>% w_median(age, weights = sampling_weight)
#>
#> Weighted Median Statistics
#> --------------------------
#> Weights: sampling_weight
#>
#> -------------------------------
#> Variable Median N Missing
#> -------------------------------
#> age 50.000 2516 0
#> -------------------------------
# Multiple variables
survey_data %>% w_median(age, income, weights = sampling_weight)
#>
#> Weighted Median Statistics
#> --------------------------
#> Weights: sampling_weight
#>
#> ---------------------------------
#> Variable Median N Missing
#> ---------------------------------
#> age 50.000 2516 0
#> income 3500.000 2201 315
#> ---------------------------------
# Grouped data
survey_data %>% group_by(region) %>% w_median(age, weights = sampling_weight)
#>
#> Weighted Median Statistics
#> --------------------------
#> Weights: sampling_weight
#>
#> Group: region = East
#> --------------------
#>
#> ------------------------------
#> Variable Median N Missing
#> ------------------------------
#> age 53.000 509 0
#> ------------------------------
#>
#> Group: region = West
#> --------------------
#>
#> -------------------------------
#> Variable Median N Missing
#> -------------------------------
#> age 49.000 2007 0
#> -------------------------------
# In summarise context
survey_data %>% summarise(med_age = w_median(age, weights = sampling_weight))
#> # A tibble: 1 × 1
#> med_age
#> <dbl>
#> 1 50
# Unweighted (for comparison)
survey_data %>% w_median(age)
#>
#> Median Statistics
#> -----------------
#>
#> -------------------------------
#> Variable Median N Missing
#> -------------------------------
#> age 50.000 2500 0
#> -------------------------------
