powered by
This function calculates the weighted mean of a numeric variable. Uses standard weighted mean formula: sum(x * w) / sum(w)
weighted_mean(data, target_col, weight_col)
Numeric weighted mean
A data.frame containing survey data
Character string specifying column name for target variable
Character string specifying column name containing weights
data <- data.frame(income = c(50000, 75000, 100000), weight = c(1.2, 0.8, 1.0)) weighted_income <- weighted_mean(data, "income", "weight")
Run the code above in your browser using DataLab