Skip to contents

The goal of infoelectoral is to facilitate the downloading and necessary processing of official data on electoral results from the Ministry of the Interior of Spain. Files can be downloaded at the polling station and municipality level on the ministry’s website. However, the format of the files (plain text without delimitation) and the data structure design (the useful information for the analysis of the elections is dispersed in at least three different files) make importing the data somewhat costly.

To download the data at the municipality level, you have the municipalities () function to which you have to provide the type of desired election (“congreso”, “senado”, “municipales” or “europeas”, the Ministry of the Interior does not have the data from the regional electoral processes), the year (YYYY) and the month (MM) of the election.

library(infoelectoral)
library(dplyr)
df <- municipios(tipo_eleccion = "congreso", anno = "1982", mes = "10")
glimpse(df)
## Rows: 119,188
## Columns: 34
## $ tipo_eleccion             <chr> "02", "02", "02", "02", "02", "02", "02", "0…
## $ anno                      <chr> "1982", "1982", "1982", "1982", "1982", "198…
## $ mes                       <chr> "10", "10", "10", "10", "10", "10", "10", "1…
## $ vuelta                    <chr> "1", "1", "1", "1", "1", "1", "1", "1", "1",…
## $ codigo_ccaa               <chr> "01", "01", "01", "01", "01", "01", "01", "0…
## $ codigo_provincia          <chr> "04", "04", "04", "04", "04", "04", "04", "0…
## $ codigo_municipio          <chr> "001", "001", "001", "001", "001", "001", "0…
## $ municipio                 <chr> "Abla", "Abla", "Abla", "Abla", "Abla", "Abl…
## $ codigo_distrito           <chr> "99", "99", "99", "99", "99", "99", "99", "9…
## $ codigo_distrito_electoral <chr> "0", "0", "0", "0", "0", "0", "0", "0", "0",…
## $ codigo_partido_judicial   <chr> "013", "013", "013", "013", "013", "013", "0…
## $ codigo_diputacion         <chr> "013", "013", "013", "013", "013", "013", "0…
## $ codigo_comarca            <chr> "000", "000", "000", "000", "000", "000", "0…
## $ poblacion_derecho         <dbl> 1723, 1723, 1723, 1723, 1723, 1723, 1723, 17…
## $ numero_mesas              <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ censo_ine                 <dbl> 1226, 1226, 1226, 1226, 1226, 1226, 1226, 12…
## $ censo_escrutinio          <dbl> 1226, 1226, 1226, 1226, 1226, 1226, 1226, 12…
## $ censo_cere                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ votantes_cere             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ participacion_1           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ participacion_2           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ votos_blancos             <dbl> 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,…
## $ votos_nulos               <dbl> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,…
## $ votos_candidaturas        <dbl> 983, 983, 983, 983, 983, 983, 983, 983, 983,…
## $ numero_concejales         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ codigo_partido_nacional   <chr> "000005", "000015", "000019", "000033", "000…
## $ codigo_partido_autonomia  <chr> "000005", "000015", "000019", "000033", "000…
## $ codigo_partido_provincia  <chr> "000005", "000015", "000019", "000033", "000…
## $ codigo_partido            <chr> "000005", "000015", "000019", "000033", "000…
## $ denominacion              <chr> "ALIANZA POPULAR - PARTIDO DEMOCRATA POPULAR…
## $ siglas                    <chr> "AP-PDP", "CDS", "CUC", "FN", "LCR", "MCA", …
## $ votos                     <dbl> 134, 6, 0, 2, 0, 0, 2, 5, 2, 1, 7, 365, 0, 4…
## $ datos_oficiales           <chr> "N", "N", "N", "N", "N", "N", "N", "N", "N",…
## $ concejales_obtenidos      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…

To download election data at the polling station level:

df <- mesas("congreso", "2019", "04")
glimpse(df)
## Rows: 712,171
## Columns: 28
## $ tipo_eleccion            <chr> "02", "02", "02", "02", "02", "02", "02", "02…
## $ anno                     <chr> "2019", "2019", "2019", "2019", "2019", "2019…
## $ mes                      <chr> "04", "04", "04", "04", "04", "04", "04", "04…
## $ vuelta                   <chr> "1", "1", "1", "1", "1", "1", "1", "1", "1", …
## $ codigo_ccaa              <chr> "01", "01", "01", "01", "01", "01", "01", "01…
## $ codigo_provincia         <chr> "04", "04", "04", "04", "04", "04", "04", "04…
## $ codigo_municipio         <chr> "003", "003", "003", "003", "003", "003", "00…
## $ municipio                <chr> "Adra", "Adra", "Adra", "Adra", "Adra", "Adra…
## $ codigo_distrito          <chr> "01", "01", "01", "01", "01", "01", "01", "01…
## $ codigo_seccion           <chr> "004", "004", "004", "004", "004", "004", "00…
## $ codigo_mesa              <chr> "B", "B", "B", "B", "B", "B", "B", "B", "B", …
## $ censo_ine                <dbl> 841, 841, 841, 841, 841, 841, 841, 841, 841, …
## $ censo_cera               <dbl> 841, 841, 841, 841, 841, 841, 841, 841, 841, …
## $ censo_cere               <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ votantes_cere            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ participacion_1          <dbl> 310, 310, 310, 310, 310, 310, 310, 310, 310, …
## $ participacion_2          <dbl> 435, 435, 435, 435, 435, 435, 435, 435, 435, …
## $ votos_blancos            <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, …
## $ votos_nulos              <dbl> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, …
## $ votos_candidaturas       <dbl> 547, 547, 547, 547, 547, 547, 547, 547, 547, …
## $ codigo_partido_nacional  <chr> "000117", "000028", "000104", "000083", "0000…
## $ codigo_partido_autonomia <chr> "000117", "000028", "000104", "000083", "0000…
## $ codigo_partido_provincia <chr> "000117", "000028", "000104", "000083", "0000…
## $ codigo_partido           <chr> "000117", "000028", "000104", "000083", "0000…
## $ denominacion             <chr> "VOX", "ESCAÑOS EN BLANCO", "RECORTES CERO-GR…
## $ siglas                   <chr> "VOX", "EB", "RECORTES CE", "PP", "PSOE", "PO…
## $ votos                    <dbl> 155, 2, 1, 89, 183, 35, 78, 3, 1, 0, 122, 0, …
## $ datos_oficiales          <chr> "S", "S", "S", "S", "S", "S", "S", "S", "S", …

The infoelectoral library also has the function candidatos(), which allows the downloading of data on candidates for the Senate, the Congress of Deputies, the European Parliament and city councils. In the case of Senate candidates, the function requires a value in the argument nivel indicating whether the results are wanted at the municipality or polling station level, and the resulting data.frame will include a column called votos indicating the number of ballots received by each candidate for the Upper House.

df <- candidatos(tipo_eleccion = "senado", anno = "2019", mes = "11", nivel = "municipio")
glimpse(df)
## Rows: 195,543
## Columns: 39
## $ tipo_eleccion             <chr> "03", "03", "03", "03", "03", "03", "03", "0…
## $ anno                      <chr> "2019", "2019", "2019", "2019", "2019", "201…
## $ mes                       <chr> "11", "11", "11", "11", "11", "11", "11", "1…
## $ vuelta                    <chr> "1", "1", "1", "1", "1", "1", "1", "1", "1",…
## $ codigo_ccaa               <chr> "14", "14", "14", "14", "14", "14", "14", "1…
## $ codigo_provincia          <chr> "01", "01", "01", "01", "01", "01", "01", "0…
## $ codigo_distrito_electoral <chr> "0", "0", "0", "0", "0", "0", "0", "0", "0",…
## $ codigo_municipio          <chr> "001", "001", "001", "001", "001", "001", "0…
## $ municipio                 <chr> "Alegría-Dulantzi", "Alegría-Dulantzi", "Ale…
## $ codigo_distrito           <chr> "99", "99", "99", "99", "99", "99", "99", "9…
## $ numero_mesas              <dbl> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,…
## $ poblacion_derecho         <dbl> 2887, 2887, 2887, 2887, 2887, 2887, 2887, 28…
## $ censo_ine                 <dbl> 2020, 2020, 2020, 2020, 2020, 2020, 2020, 20…
## $ censo_escrutinio          <dbl> 2020, 2020, 2020, 2020, 2020, 2020, 2020, 20…
## $ censo_cere                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ votantes_cere             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ participacion_1           <dbl> 666, 666, 666, 666, 666, 666, 666, 666, 666,…
## $ participacion_2           <dbl> 1099, 1099, 1099, 1099, 1099, 1099, 1099, 10…
## $ votos_blancos             <dbl> 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, …
## $ votos_nulos               <dbl> 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, …
## $ votos_candidaturas        <dbl> 1334, 1334, 1334, 1334, 1334, 1334, 1334, 13…
## $ codigo_partido_nacional   <chr> "000029", "000018", "000055", "000078", "000…
## $ codigo_partido_autonomia  <chr> "000029", "000018", "000055", "000077", "000…
## $ codigo_partido_provincia  <chr> "000029", "000018", "000055", "000077", "000…
## $ codigo_partido            <chr> "000029", "000018", "000055", "000077", "000…
## $ denominacion              <chr> "denominacion", "denominacion", "denominacio…
## $ siglas                    <chr> "EH Bildu", "Cs", "PACMA", "PODEMOS-IU", "PS…
## $ codigo_senador            <chr> "019012", "019018", "019021", "019007", "019…
## $ orden_candidato           <dbl> 3, 3, 2, 1, 2, 1, 1, 2, 3, 1, 3, 2, 2, 1, 1,…
## $ tipo_candidato            <chr> "T", "T", "T", "T", "T", "T", "T", "T", "T",…
## $ nombre                    <chr> "Estíbaliz", "Eusebio", "Sergio", "María Con…
## $ apellido_1                <chr> "Ibáñez de Gauna", "Requejo", "Rodríguez", "…
## $ apellido_2                <chr> "Garaygordobil", "Moratinos", "Rodríguez", "…
## $ sexo                      <chr> "F", "M", "M", "F", "M", "F", "M", "M", "M",…
## $ nacimiento                <date> 2024-03-08, 2024-03-08, 2024-03-08, 2024-03…
## $ dni                       <chr> "0000000000", "0000000000", "0000000000", "0…
## $ votos                     <dbl> 235, 17, 17, 227, 258, 3, 52, 365, 101, 276,…
## $ electo                    <chr> "N", "N", "N", "N", "N", "N", "N", "S", "N",…
## $ datos_oficiales           <chr> "S", "S", "S", "S", "S", "S", "S", "S", "S",…

In the case of candidates for the Congress of Deputies, European Parliament or city councils, institutions elected through closed lists, it is not necessary to provide a value for the argument nivel and the resulting object will not have the column votos.

df <- candidatos("europeas", "2019", "05")
glimpse(df)
## Rows: 1,728
## Columns: 22
## $ tipo_eleccion            <chr> "07", "07", "07", "07", "07", "07", "07", "07…
## $ anno                     <chr> "2019", "2019", "2019", "2019", "2019", "2019…
## $ mes                      <chr> "05", "05", "05", "05", "05", "05", "05", "05…
## $ vuelta                   <chr> "1", "1", "1", "1", "1", "1", "1", "1", "1", …
## $ codigo_provincia         <chr> "99", "99", "99", "99", "99", "99", "99", "99…
## $ codigo_municipio         <chr> "999999", "999999", "999999", "999999", "9999…
## $ codigo_distrito          <chr> "99", "99", "99", "99", "99", "99", "99", "99…
## $ orden_candidato          <dbl> 35, 47, 22, 23, 43, 24, 21, 34, 2, 48, 49, 42…
## $ tipo_candidato           <chr> "T", "T", "T", "T", "T", "T", "T", "T", "T", …
## $ nombre                   <chr> "Dionisio", "María Luisa", "Emilia", "Ana Mar…
## $ apellido_1               <chr> "Solana", "Salvador", "Baena", "Miguel", "Cic…
## $ apellido_2               <chr> "Checa", "Vicente", "Capilla", "Minguez", "Mi…
## $ sexo                     <chr> "M", "F", "F", "F", "F", "M", "F", "M", "F", …
## $ nacimiento               <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ dni                      <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ electo                   <chr> "N", "N", "N", "N", "N", "N", "N", "N", "N", …
## $ codigo_partido_nacional  <chr> "000001", "000001", "000001", "000001", "0000…
## $ codigo_partido_autonomia <chr> "000001", "000001", "000001", "000001", "0000…
## $ codigo_partido_provincia <chr> "000001", "000001", "000001", "000001", "0000…
## $ codigo_partido           <chr> "000001", "000001", "000001", "000001", "0000…
## $ denominacion             <chr> "denominacion", "denominacion", "denominacion…
## $ siglas                   <chr> "I.Fem", "I.Fem", "I.Fem", "I.Fem", "I.Fem", …