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> "14", "14", "14", "14", "14", "14", "14", "1…
## $ codigo_provincia          <chr> "01", "01", "01", "01", "01", "01", "01", "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…
## $ codigo_distrito_electoral <chr> "0", "0", "0", "0", "0", "0", "0", "0", "0",…
## $ codigo_partido_judicial   <chr> "059", "059", "059", "059", "059", "059", "0…
## $ codigo_diputacion         <chr> "059", "059", "059", "059", "059", "059", "0…
## $ codigo_comarca            <chr> "000", "000", "000", "000", "000", "000", "0…
## $ poblacion_derecho         <dbl> 1037, 1037, 1037, 1037, 1037, 1037, 1037, 10…
## $ numero_mesas              <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ censo_ine                 <dbl> 694, 694, 694, 694, 694, 694, 694, 694, 694,…
## $ censo_escrutinio          <dbl> 694, 694, 694, 694, 694, 694, 694, 694, 694,…
## $ 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> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,…
## $ votos_nulos               <dbl> 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, …
## $ votos_candidaturas        <dbl> 556, 556, 556, 556, 556, 556, 556, 556, 556,…
## $ numero_concejales         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ codigo_partido_nacional   <chr> "000082", "000015", "000020", "000080", "000…
## $ codigo_partido_autonomia  <chr> "000082", "000015", "000020", "000080", "000…
## $ codigo_partido_provincia  <chr> "000082", "000015", "000020", "000080", "000…
## $ codigo_partido            <chr> "000082", "000015", "000020", "000080", "000…
## $ denominacion              <chr> "UNIFICACION COMUNISTA DE ESPAÑA", "CENTRO D…
## $ siglas                    <chr> "UCE", "CDS", "EAJ-PNV", "SE", "PCE-EPK", "P…
## $ votos                     <dbl> 0, 9, 169, 1, 11, 4, 4, 0, 0, 0, 87, 0, 0, 2…
## $ 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> "14", "14", "14", "14", "14", "14", "14", "14…
## $ codigo_provincia         <chr> "01", "01", "01", "01", "01", "01", "01", "01…
## $ codigo_municipio         <chr> "001", "001", "001", "001", "001", "001", "00…
## $ municipio                <chr> "Alegría-Dulantzi", "Alegría-Dulantzi", "Aleg…
## $ codigo_distrito          <chr> "01", "01", "01", "01", "01", "01", "01", "01…
## $ codigo_seccion           <chr> "001", "002", "002", "001", "002", "001", "00…
## $ codigo_mesa              <chr> "B", "A", "B", "B", "A", "B", "A", "A", "B", …
## $ censo_ine                <dbl> 492, 512, 518, 492, 512, 492, 483, 512, 518, …
## $ censo_cera               <dbl> 492, 512, 518, 492, 512, 492, 483, 512, 518, …
## $ 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> 221, 219, 218, 221, 219, 221, 141, 219, 218, …
## $ participacion_2          <dbl> 309, 334, 328, 309, 334, 309, 278, 334, 328, …
## $ votos_blancos            <dbl> 2, 1, 0, 2, 1, 2, 1, 1, 0, 2, 1, 2, 0, 0, 1, …
## $ votos_nulos              <dbl> 5, 6, 2, 5, 6, 5, 6, 6, 2, 5, 6, 5, 2, 2, 6, …
## $ votos_candidaturas       <dbl> 355, 353, 377, 355, 353, 355, 330, 353, 377, …
## $ codigo_partido_nacional  <chr> "000066", "000033", "000083", "000083", "0001…
## $ codigo_partido_autonomia <chr> "000067", "000033", "000083", "000083", "0001…
## $ codigo_partido_provincia <chr> "000067", "000033", "000083", "000083", "0001…
## $ codigo_partido           <chr> "000067", "000033", "000083", "000083", "0001…
## $ denominacion             <chr> "PARTIDO COMUNISTA DE LOS TRABAJADORES DE EUS…
## $ siglas                   <chr> "PCTE/ELAK", "EH Bildu", "PP", "PP", "VOX", "…
## $ votos                    <dbl> 0, 59, 35, 18, 10, 108, 52, 85, 1, 69, 13, 5,…
## $ 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", "002", "003", "004", "006", "008", "0…
## $ municipio                 <chr> "Alegría-Dulantzi", "Amurrio", "Aramaio", "A…
## $ codigo_distrito           <chr> "99", "99", "99", "99", "99", "99", "99", "9…
## $ numero_mesas              <dbl> 4, 15, 2, 2, 1, 1, 2, 5, 1, 1, 1, 1, 1, 2, 1…
## $ poblacion_derecho         <dbl> 2887, 10286, 1483, 1831, 219, 982, 1643, 296…
## $ censo_ine                 <dbl> 2020, 8014, 1166, 1357, 174, 808, 1258, 2315…
## $ censo_escrutinio          <dbl> 2020, 8014, 1166, 1357, 174, 808, 1258, 2315…
## $ 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, 3468, 509, 548, 65, 317, 484, 966, 93, …
## $ participacion_2           <dbl> 1099, 4770, 691, 768, 95, 436, 674, 1420, 13…
## $ votos_blancos             <dbl> 10, 71, 7, 11, 3, 11, 10, 18, 6, 3, 6, 3, 5,…
## $ votos_nulos               <dbl> 40, 120, 14, 27, 3, 10, 16, 39, 2, 7, 9, 5, …
## $ votos_candidaturas        <dbl> 1334, 5631, 822, 908, 116, 525, 811, 1644, 1…
## $ codigo_partido_nacional   <chr> "000018", "000018", "000018", "000018", "000…
## $ codigo_partido_autonomia  <chr> "000018", "000018", "000018", "000018", "000…
## $ codigo_partido_provincia  <chr> "000018", "000018", "000018", "000018", "000…
## $ codigo_partido            <chr> "000018", "000018", "000018", "000018", "000…
## $ denominacion              <chr> "CIUDADANOS-PARTIDO DE LA CIUDADANÍA", "CIUD…
## $ siglas                    <chr> "Cs", "Cs", "Cs", "Cs", "Cs", "Cs", "Cs", "C…
## $ codigo_senador            <chr> "019016", "019016", "019016", "019016", "019…
## $ orden_candidato           <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ tipo_candidato            <chr> "T", "T", "T", "T", "T", "T", "T", "T", "T",…
## $ nombre                    <chr> "Cristian", "Cristian", "Cristian", "Cristia…
## $ apellido_1                <chr> "Martínez", "Martínez", "Martínez", "Martíne…
## $ apellido_2                <chr> "Conca", "Conca", "Conca", "Conca", "Conca",…
## $ sexo                      <chr> "M", "M", "M", "M", "M", "M", "M", "M", "M",…
## $ nacimiento                <date> 2022-07-13, 2022-07-13, 2022-07-13, 2022-07…
## $ dni                       <chr> "0000000000", "0000000000", "0000000000", "0…
## $ votos                     <dbl> 24, 39, 0, 9, 2, 3, 3, 10, 3, 2, 5, 3, 4, 11…
## $ electo                    <chr> "N", "N", "N", "N", "N", "N", "N", "N", "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: 9,859
## 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> "01", "01", "01", "01", "01", "01", "01", "01…
## $ codigo_municipio         <chr> "001", "002", "003", "004", "006", "008", "00…
## $ codigo_distrito          <chr> "99", "99", "99", "99", "99", "99", "99", "99…
## $ orden_candidato          <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ tipo_candidato           <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ nombre                   <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ apellido_1               <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ apellido_2               <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ sexo                     <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ 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> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ codigo_partido_nacional  <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ codigo_partido_autonomia <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ codigo_partido_provincia <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ codigo_partido           <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ denominacion             <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ siglas                   <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…