Title: German Election Database (GERDA)
Version: 0.6.0
Author: Hanno Hilbig ORCID iD [aut, cre]
Description: Provides tools to download datasets of German elections covering local, state, federal, mayoral, European Parliament, and county (Kreistag) elections, with federal county-level coverage from 1953 and other families extending through 2025. The package supplies turnout, vote shares, and derived indicators at the municipal and county level, including geographically harmonized datasets that account for changes in municipal boundaries over time and incorporate mail-in voting districts. Bundled data includes county-level INKAR covariates (1995-2022) and municipality-level Zensus 2022 indicators. Data is sourced from https://github.com/awiedem/german_election_data.
License: MIT + file LICENSE
Encoding: UTF-8
RoxygenNote: 7.3.3
Depends: R (≥ 3.5.0)
Imports: dplyr, readr, stats, stringdist, tibble
Suggests: knitr, rmarkdown, testthat (≥ 3.0.0)
Config/testthat/edition: 3
URL: https://github.com/hhilbig/gerda, https://github.com/awiedem/german_election_data
BugReports: https://github.com/hhilbig/gerda/issues
VignetteBuilder: knitr
Maintainer: Hanno Hilbig <hhilbig@ucdavis.edu>
NeedsCompilation: no
Packaged: 2026-04-19 23:22:52 UTC; hanno
Repository: CRAN
Date/Publication: 2026-04-20 08:30:08 UTC

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:

Usage

add_gerda_census(election_data)

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).

Details

Required Columns

The input data must contain one of:

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:

Value

The input data frame with additional census columns appended. The number of rows remains unchanged (left join).

See Also

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)


Add County-Level Covariates to GERDA Election Data

Description

Convenience function to merge INKAR county-level (Kreis) covariates with GERDA election data. This is the recommended way to add covariates, as it automatically uses the correct join keys and prevents common merge errors.

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

Important: Covariates are always at the county level. When merging with municipal data, all municipalities within the same county will receive identical covariate values.

The function performs a left join, keeping all rows from the election data and adding covariates where available. This automatically retains only election years.

Usage

add_gerda_covariates(election_data)

Arguments

election_data

A data frame containing GERDA election data. Must contain a column with county or municipal codes (see Details) and election_year.

Details

Required Columns

The input data must contain election_year and one of:

The function automatically detects which column is present and performs the appropriate merge. For municipal data, the county code is extracted from the first 5 digits of the AGS.

Data Level

Covariates are at the county (Kreis) level:

Data Availability

Covariates are available from 1995-2022. For GERDA federal elections:

Missing Data

Some covariates have missing values. Use gerda_covariates_codebook() to check data availability for specific variables.

Value

The input data frame with additional columns for all 30 county-level covariates. The number of rows remains unchanged (left join).

See Also

Examples

## Not run: 
library(gerda)
library(dplyr)

# Example 1: County-level election data
county_data <- load_gerda_web("federal_cty_harm") %>%
  add_gerda_covariates()

# Check the result
names(county_data) # See new covariate columns
table(county_data$election_year) # Only election years

# Example 2: Municipal-level election data
# Note: All municipalities in the same county will get identical covariates
muni_data <- load_gerda_web("federal_muni_harm_21") %>%
  add_gerda_covariates()

# Verify: municipalities in same county have same covariate values.
# The county code is the first 5 digits of the 8-digit municipal AGS.
muni_data %>%
  mutate(county_code = substr(ags, 1, 5)) %>%
  group_by(county_code, election_year) %>%
  summarize(
    n_munis = n(),
    unemp_range = max(unemployment_rate) - min(unemployment_rate)
  )

# Analyze with covariates
county_data %>%
  filter(election_year == 2021) %>%
  filter(!is.na(unemployment_rate)) %>%
  summarize(cor_unemployment_afd = cor(unemployment_rate, afd))

## End(Not run)


Get Municipality-Level Census 2022 Data

Description

Returns municipality-level demographic and socioeconomic data from the German Census 2022 (Zensus 2022). This is a cross-sectional snapshot covering all German municipalities.

For most users, we recommend using add_gerda_census instead, which automatically merges census data with GERDA election data.

Usage

gerda_census()

Details

The dataset includes:

Municipality codes are 8-digit AGS codes. Since the census is a single 2022 snapshot, there is no year dimension.

Value

A data frame with approximately 10,800 rows (one per municipality) and 16 columns containing census indicators. See gerda_census_codebook for variable descriptions.

See Also

Examples

# Get the census data
census <- gerda_census()
head(census)

# Check available municipalities
nrow(census)


Get Codebook for Census 2022 Data

Description

Returns the data dictionary for municipality-level Census 2022 indicators. Provides variable names, labels, units, and data sources.

Usage

gerda_census_codebook()

Value

A data frame with 16 rows documenting all variables in the census dataset.

See Also

gerda_census for the actual census data

Examples

# View the codebook
codebook <- gerda_census_codebook()
print(codebook)


Get County-Level Covariates from INKAR

Description

Returns county-level socioeconomic and demographic covariates from INKAR. This function provides flexible access to the raw covariate data for advanced users who want to inspect or manipulate it before merging with county-level election data.

For most users, we recommend using add_gerda_covariates instead, which automatically performs the merge with correct join keys.

Note: These covariates are at the county (Kreis) level and should be merged with county-level GERDA data (e.g., federal_cty_harm).

Usage

gerda_covariates()

Details

The dataset includes 30 socioeconomic and demographic variables:

County codes are formatted as 5-digit AGS codes matching GERDA's harmonized county codes (2021 boundaries).

Value

A data frame with 11,200 rows and 32 columns containing county-level covariates for 400 German counties from 1995 to 2022. See gerda_covariates_codebook for variable descriptions.

See Also

Examples

# Get the covariates data (bundled, no network call)
covs <- gerda_covariates()

# Inspect the data
head(covs)
summary(covs)


# Manual merge (advanced) — downloads election data from GitHub
library(dplyr)
elections <- load_gerda_web("federal_cty_harm")
merged <- elections %>%
  left_join(covs, by = c("county_code" = "county_code", "election_year" = "year"))



Get Codebook for County-Level Covariates

Description

Returns the data dictionary for county-level (Kreis) covariates from INKAR. Provides variable names, labels, units, categories, original INKAR codes, and missing data information for all county-level socioeconomic and demographic indicators.

Usage

gerda_covariates_codebook()

Value

A data frame with 32 rows documenting all variables in the county covariates dataset.

See Also

gerda_covariates for the actual covariate data

Examples

# View the full codebook
codebook <- gerda_covariates_codebook()
print(codebook)

# Find variables by category
library(dplyr)
codebook %>%
  filter(category == "Demographics")

# Find variables with good coverage
codebook %>%
  filter(missing_pct < 5)


List of GERDA Data

Description

This function lists the available GERDA data sets. The purpose of this function is to quickly provide a list of available data sets and their descriptions.

Usage

gerda_data_list(print_table = TRUE)

Arguments

print_table

A logical value indicating whether to print the table in the console (TRUE) or return the data as a tibble (FALSE). Default is TRUE.

Details

In addition to downloadable datasets, the package includes bundled covariate data accessible via dedicated functions:

Value

A tibble containing the available GERDA data with descriptions. When print_table = TRUE, the function prints a formatted table to the console and invisibly returns the data tibble. When print_table = FALSE, the function directly returns the data tibble.

Examples

gerda_data_list()


Load GERDA Data

Description

This function loads GERDA data from a web source.

Usage

load_gerda_web(
  file_name,
  verbose = FALSE,
  file_format = "rds",
  on_error = getOption("gerda.on_error", "warn")
)

Arguments

file_name

A character string specifying the name of the file to load. For a list of available data, see gerda_data_list.

verbose

A logical value indicating whether to print additional messages to the console. Default is FALSE.

file_format

A character string specifying the format of the file. Must be either "csv" or "rds". Default is "rds".

on_error

How to handle errors (unknown dataset name, failed download, corrupt file, invalid file_format). Either "warn" (default) to emit a warning and return NULL, or "stop" to raise an error. Use "stop" inside scripts or pipelines where silent NULL returns would produce confusing downstream failures. The global default can also be overridden with options(gerda.on_error = "stop").

Value

A tibble containing the loaded data, or NULL if the data could not be loaded.

Vote-share columns

Election datasets expose one column per party (e.g. cdu, spd, gruene, afd). These columns hold the party's share of valid votes and are expressed as fractions of 1. They do not sum to 1 across the named major parties: the remainder is held by smaller parties with their own columns and, at the tail, an other category. For example, in federal_cty_harm for 2021, cdu + csu + spd + gruene + fdp + linke_pds + afd is typically around 0.91 and ranges roughly 0.78 to 0.97 across counties. To reconstruct a full 1.0 share, include every party column or use other together with turnout and invalid-vote columns.

Examples


# Load harmonized municipal elections data
data_municipal_harm <- load_gerda_web("municipal_harm", verbose = TRUE, file_format = "rds")

# Load federal election data harmonized to 2025 boundaries (includes 2025 election)
data_federal_2025 <- load_gerda_web("federal_muni_harm_25", verbose = TRUE, file_format = "rds")



Map GERDA Party Names to ParlGov Attributes

Description

Creates a crosswalk between GERDA party names and ParlGov's view_party attributes. If a party name is not found, the corresponding output element is NA. This function expects GERDA party names (lowercase, underscores); other naming schemes will mostly return NA.

Usage

party_crosswalk(party_gerda, destination)

Arguments

party_gerda

A character vector containing the GERDA party names to be converted.

destination

A single string naming the target column. Available destinations:

  • Names: party_name, party_name_ascii, party_name_short, party_name_english

  • Party family: family_name, family_name_short

  • Ideology scales (ParlGov): left_right, state_market, liberty_authority, eu_anti_pro

  • External ideology scores: cmp, euprofiler, ees, castles_mair, huber_inglehart, ray, benoit_laver, chess

  • Identifiers: country_id, party_id, family_id

Value

A vector of the same length as party_gerda with the mapped values.

Examples

party_crosswalk(c("cdu", "spd", "linke_pds", NA), "left_right")
party_crosswalk(c("cdu", "afd"), "family_name_short")