Skip to contents

Compute Weighted Mean Across a List of Data Frames (with optional grouping)

Usage

run_weighted_mean(
  data_list,
  var_name,
  wgt_name = NULL,
  na.rm = TRUE,
  by = NULL
)

Arguments

data_list

A named list of data frames, (e.g., across countries or years).

var_name

A string specifying the variable name (e.g., "dhi", "pilabour") to compute the mean on.

wgt_name

An optional string specifying the weight variable to be used. If NULL, equal weights are assumed.

na.rm

Logical. If TRUE, missing values in var_name or wgt_name are removed.

by

Optional string giving the name of a categorical variable to split the data within each data frame before computing the mean.

Value

A named list of data frames.

Examples

if (FALSE) { # \dontrun{
data <- lissyrtools::lissyuse(data = c("de", "es", "uk"), vars = c("dhi", "age", "pi11", "region_c", "area_c", "educ", "emp"), from = 2016)

data %>% 
purrr::map(~ .x %>% filter(relation == 1000)) %>%
run_weighted_mean("dhi", "hpopwgt")

data %>% 
purrr::map(~ .x %>% filter(age > 25 & age <65)) %>%
run_weighted_mean("pi11", "ppopwgt", by = "educ")

} # }