Type: Package
Title: Download Spanish Election Results
Version: 1.0.3
Maintainer: Héctor Meleiro <hmeleiros@gmail.com>
Description: Download official election results for Spain at polling station, municipality and province level, format them and import them to the R environment. Data are provided by the Spanish Ministry of the Interior (https://infoelectoral.interior.gob.es/es/inicio/).
Depends: R (≥ 3.5.0)
License: GPL-2
URL: https://github.com/rOpenSpain/infoelectoral
BugReports: https://github.com/rOpenSpain/infoelectoral/issues
Encoding: UTF-8
LazyData: true
Imports: dplyr (≥ 1.0.0), stringr (≥ 1.0.0), httr (≥ 1.0.0)
Suggests: ggplot2, mapSpain (≥ 0.9.0), patchwork, purrr, tidyr, knitr, pkgdown, testthat (≥ 3.0.0)
RoxygenNote: 7.3.2
VignetteBuilder: knitr
Config/testthat/edition: 3
Config/Needs/website: hmeleiro/spainelectoraltheme
NeedsCompilation: no
Packaged: 2026-04-29 07:44:08 UTC; hmele
Author: Héctor Meleiro [aut, cre]
Repository: CRAN
Date/Publication: 2026-04-29 09:00:02 UTC

Download candidate data

Description

'candidatos()' downloads, formats and imports to the environment the data of the candidates from the electoral lists of the selected elections.

Usage

candidatos(tipo_eleccion, anno, mes, nivel)

Arguments

tipo_eleccion

The type of choice you want to download. The accepted values are "congreso", "senado", "europeas" o "municipales".

anno

The year of the election in YYYY format.

mes

The month of the election in MM format.

nivel

The administrative level for which the data is wanted ("mesa" for polling stations or "municipio" for municipalities). Only necessary when tipo_eleccion = "senado"

Value

data.frame with the candidates data. If tipo_eleccion = "senado" a column called 'votos' is included with the votes recieved by each candidate. If other type of election is selected this column is not included since the votes are not received by the specific candidates but by the closed list of the party.

Examples


data <- candidatos(
  tipo_eleccion = "senado", anno = "2004",
  mes = "03", nivel = "municipio"
)
str(data)


candidatos_nosenado

Description

'candidatos_nosenado()' downloads, formats and imports to the environment the data of the candidates from the electoral lists of the selected elections.

Usage

candidatos_nosenado(tipo, anno, mes)

Arguments

tipo

Code for the type of election.

anno

The year of the election in YYYY format.

mes

The month of the election in MM format.

Value

data.frame with the data of candidates.


candidatos_senado

Description

'candidatos_senado()' downloads, formats and imports to the environment the data of the Senate candidates of the selected elections.

Usage

candidatos_senado(anno, mes, nivel)

Arguments

anno

The year of the election in YYYY format.

mes

The month of the election in MM format.

nivel

The administrative level for which the data is wanted ("mesa" for polling stations or "municipio" for municipalities).

Value

data.frame with the data for the Senate candidates.


Cleans up the files and directories created during the process

Description

Cleans up the files and directories created during the process

Usage

cleanup(dirs)

Arguments

dirs

The directories to remove.


Administrative codes for spanish autonomous communities.

Description

This dataset contains the codes given to the autonomus communities by the Ministry of Interior and the ones given by the National Institute of Statistics with their official names.

Usage

codigos_ccaa

Format

A dataset with 19 rows and 3 columns:

codigo_ccaa

Code given to the autonomous communities by the Ministry of Interior (not the same as the National Institute of Statistics codes)

codigo_ccaa_ine

Code given to the autonomous communities by the National Institute of Statistics

provincia

Official name of the provinces

Source

Ministry of the Interior election data download area.


Administrative codes for spanish municipalities.

Description

This dataset contains the INE codes of the municipalities of Spain with their most recent names (eg: Cabrera d'Igualada appears as Cabrera d'Anoia). For the municipalities that have been merged at some point, their codes are kept separately along with that of the new municipality created (eg: it contains the municipality Oza-Cesuras but also that of Cesuras and Oza dos Ríos separately).

Usage

codigos_municipios

Format

A dataset with more than 8.000 rows and 3 columns:

codigo_provincia

Code given to the provinces by the National Institute of Statistics

codigo_municipio

Code given to the municipalities by the National Institute of Statistics

municipio

Most recent official name of the municipality

Source

National Statistics Institute (INE) municipality code portal.


Recoded party names

Description

This dataset contains a list of recoded electoral party or coalition names with their correspondent national codes. For example: 'PSOE' when the original name is 'PSA-PSOE', 'PSOE-PROGR.' or 'PSOE-A'. This recodification helps the longitudinal analysis of the electoral results, avoiding the many variations in the party and coalition names.

Usage

codigos_partidos

Format

A dataset with the names of the electoral party or coalition

anno

Year of the election

mes

Month of the election

codigo_partido_nacional

The national accumulation code for the electoral party or coalition

partido

The recoded name for the electoral party or coalition.


Administrative codes for spanish provinces.

Description

This dataset contains the National Institute of Statistics administrative codes for spanish provinces with their official names.

Usage

codigos_provincias

Format

A dataset with 52 rows and 5 columns:

codigo_ccaa

Code given to the autonomous communities by the Ministry of Interior (not the same as the National Institute of Statistics codes)

codigo_ccaa_ine

Code given to the autonomous communities by the National Institute of Statistics

ccaa

Official name of the autonomous communities

codigo_provincia

Code given to the provinces by the National Institute of Statistics

provincia

Official name of the provinces

Source

https://www.ine.es/daco/daco42/codmun/cod_provincia.htm


Downloads the elections zip file

Description

Downloads the elections zip file

Usage

download_bin(url, tempfile)

Arguments

url

The URL of the elections zip to download

tempfile

The path to save the downloaded file


Generates the election type code from a string

Description

Generates the election type code from a string

Usage

election_type_code(tipo_eleccion)

Arguments

tipo_eleccion

The type of choice you want to download. The accepted values are "congreso", "senado", "europeas" o "municipales".

Value

A string


Election dates

Description

This dataset contains the dates of the local, general and european elections in Spain from 1977 to 2023.

Usage

fechas_elecciones

Format

A dataset with the dates of the elections

tipo_eleccion

Election level (local, general or european parliament)

anno

Year of the election

mes

Month of the election

day

Day of the election

Source

https://www.juntaelectoralcentral.es/cs/jec/elecciones


Generates the downloads URL of the election zip file

Description

Generates the downloads URL of the election zip file

Usage

generate_url(tipo, anno, mes, level)

Arguments

tipo

The code of the type of election you want to download.

anno

The year of the election in YYYY format.

mes

The month of the election in MM format.

Value

A string


Download data at the polling station level

Description

'mesas()' downloads, formats and imports to the environment the electoral results data of the selected election at the polling station level.

Usage

mesas(tipo_eleccion, anno, mes)

Arguments

tipo_eleccion

The type of choice you want to download. The accepted values are "congreso", "senado", "europeas" o "municipales".

anno

The year of the election in YYYY format.

mes

The month of the election in MM format.

Value

data.frame with the electoral results data at the polling station level.

Examples


data <- mesas(tipo_eleccion = "congreso", anno = "2023", mes = "07")
str(data)


Download data at the municipality level

Description

'municipios()' downloads, formats and imports to the environment the electoral results data of the selected election at the municipality level.

Usage

municipios(tipo_eleccion, anno, mes, distritos = FALSE)

Arguments

tipo_eleccion

The type of choice you want to download. The accepted values are "congreso", "senado", "europeas" o "municipales".

anno

The year of the election in YYYY format.

mes

The month of the election in MM format.

distritos

Should district level results be returned when available? The default is FALSE. Please be aware when summarizing the data that districts = TRUE will return separate rows for the total municipal level and for each of the districts.

Value

Dataframe with the electoral results data at the municipality level.

Examples


data <- municipios(tipo_eleccion = "congreso", anno = "2019", mes = "11")
str(data)



Download data at the electoral constituency level (province or island)

Description

'provincias()' downloads, formats and imports to the environment the electoral results data of the selected election at electoral constituency level (province or island).

Usage

provincias(tipo_eleccion, anno, mes)

Arguments

tipo_eleccion

The type of choice you want to download. The accepted values are "congreso", "senado", "europeas" o "municipales".

anno

The year of the election in YYYY format.

mes

The month of the election in MM format.

Value

data.frame with the electoral results data at the polling station level.

Examples


data <- provincias(tipo_eleccion = "congreso", anno = "1982", mes = "10")
str(data)


Reads the candidacy files

Description

Reads the candidacy files

Usage

read03(file, tempd)

Arguments

file

Path to the .DAT file that begins with 03.

Value

data.frame


Reads the candidates info files

Description

Reads the candidates info files

Usage

read04(file, tempd)

Arguments

file

Path to the .DAT file that begins with 04.

Value

data.frame


Reads the basic municipal data files

Description

Reads the basic municipal data files

Usage

read05(file, tempd)

Arguments

file

Path to the .DAT file that begins with 05.

@return data.frame


Reads the candidacy info at municipality level files

Description

Reads the candidacy info at municipality level files

Usage

read06(file, tempd)

Arguments

file

Path to the .DAT file that begins with 06.

@return data.frame


Reads the basic data files for the area superior to the municipality (provinces, islands, etc...)

Description

Reads the basic data files for the area superior to the municipality (provinces, islands, etc...)

Usage

read07(file, tempd)

Arguments

file

Path to the .DAT file that begins with 07.

Value

data.frame


Reads the candidacy files at the area superior to the municipality (provinces, islands, etc...)

Description

Reads the candidacy files at the area superior to the municipality (provinces, islands, etc...)

Usage

read08(file, tempd)

Arguments

file

Path to the .DAT file that begins with 08.

Value

data.frame


Reads the basic polling station data files

Description

Reads the basic polling station data files

Usage

read09(file, tempd)

Arguments

file

Path to the .DAT file that begins with 09.

Value

data.frame


Reads the candidacy info at polling station level files

Description

Reads the candidacy info at polling station level files

Usage

read10(file, tempd)

Arguments

file

Path to the .DAT file that begins with 10.

Value

data.frame


Reads the basic data files for municipalities of 250 inhabitants or less

Description

Reads the basic data files for municipalities of 250 inhabitants or less

Usage

read11(file, tempd)

Arguments

file

Path to the .DAT file that begins with 11.

@return data.frame


Reads the candidacy files for small municipalities

Description

Reads the candidacy files for small municipalities

Usage

read12(file, tempd)

Arguments

file

Path to the .DAT file that begins with 12.

Value

data.frame


Mean income at the census section level (INE)

Description

This dataset contains the mean income of each census section

Usage

renta

Format

A dataset with more than 34.000 rows and 2 columns:

codigo_seccion

Code given to the census section made by the combination of the codes of the province, the municipality, the district and the section.

renta

Mean income of the census section in euros

Source

National Statistics Institute (INE) income statistics portal.


senado_mesas

Description

'senado_mesas' downloads the Senate candidates data at the polling station level.

Usage

senado_mesas(anno, mes)

Arguments

anno

The year of the election in YYYY format.

mes

The month of the election in MM format.

Value

data.frame with the data for the Senate candidates.


senado_municipios

Description

'senado_mesas' downloads the Senate candidates data at the municipality level.

Usage

senado_municipios(anno, mes)

Arguments

anno

The year of the election in YYYY format.

mes

The month of the election in MM format.

Value

data.frame with the data for the Senate candidates.