add_gerda_census: Add Census 2022 Data to GERDA Election Data

View source: R/gerda_census.R

add_gerda_censusR Documentation

Add Census 2022 Data to GERDA Election Data

Description

Convenience function to merge Zensus 2022 municipality-level data with GERDA election data. The census provides a cross-sectional snapshot (2022), so the same values are attached to all election years.

The function works with both municipality-level and county-level election data:

  • Municipality-level data: Direct merge using 8-digit AGS codes

  • County-level data: Census data is aggregated to the county level (population-weighted means for shares, sums for counts) before merging

Usage

add_gerda_census(election_data, unmatched = "warn")

Arguments

election_data

A data frame containing GERDA election data. Must contain either an ags column (municipality level) or a county_code column (county level).

unmatched

How to handle input rows whose geographic identifier does not match Census 2022. One of "warn" (default), "error", or "ignore".

Details

Required Columns

The input data must contain one of:

  • ags: 8-digit municipal code for municipality-level data

  • county_code: 5-digit county code for county-level data

Merge Behavior

Since the census is a 2022 cross-section, census values are the same for all election years. The merge is on geography only (no year join).

For county-level data, municipality-level census data is first aggregated:

  • Share variables: Population-weighted means

  • Count variables (population_census22, total_dwellings_census22): Sums

  • Other variables (avg_household_size_census22, avg_rent_per_m2_census22): Population-weighted means

Validation and Diagnostics

Geographic identifiers must be character vectors containing exactly eight digits (ags) or five digits (county_code). Numeric identifiers are rejected because leading zeros may already have been lost. Before joining, the function verifies reference-key uniqueness and rejects output-column conflicts. It then verifies that the input row count is unchanged and reports the exact number of unmatched rows and geographic units. Missing join keys are classified as unexpected. Use unmatched = "error" in unattended pipelines.

Value

The input data frame with additional census columns appended. The number of rows remains unchanged. A machine-readable join report is attached and can be retrieved with gerda_join_diagnostics().

See Also

  • gerda_census for direct access to the census data

  • gerda_census_codebook for variable descriptions

  • gerda_join_diagnostics for inspecting match results

Examples

## Not run: 
library(gerda)

# Municipality-level merge
muni_data <- load_gerda_web("federal_muni_harm_21") |>
  add_gerda_census()

# County-level merge (aggregated from municipalities)
county_data <- load_gerda_web("federal_cty_harm") |>
  add_gerda_census()

## End(Not run)


gerda documentation built on July 27, 2026, 5:09 p.m.