Learn R Programming

SurveyStat (version 1.0.3)

weighted_mean: Calculate weighted mean

Description

This function calculates the weighted mean of a numeric variable. Uses standard weighted mean formula: sum(x * w) / sum(w)

Usage

weighted_mean(data, target_col, weight_col)

Value

Numeric weighted mean

Arguments

data

A data.frame containing survey data

target_col

Character string specifying column name for target variable

weight_col

Character string specifying column name containing weights

Examples

Run this code
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