This tutorial aims to illustrate how more advanced developers can use the helper
functions in the utils.R
script:
Convert type of election to code
According to the data extracted from the website
of the Spanish Ministry of the Interior, each type of
election is associated in the databases with a specific alphanumeric
code: "01"
(referendum), "02"
(congress), "03"
(senate), "04"
(local
elections), "06"
(cabildo - Canarian council - elections),
and "07"
(European Parliament elections). However, for
users, it’s much simpler to provide lexically explicit
terms — for example, writing "congress"
for
congressional elections or "senate"
for senatorial
elections. The type_to_code_election()
function allows us
to convert the user-provided term into the corresponding code required
by our databases.
type_to_code_election(type_elec = "congress")
#> [1] "02"
type_to_code_election(type_elec = "senate")
#> [1] "03"
type_to_code_election(type_elec = "local")
#> [1] "04"
Extract region codes
Similarly, many of the package’s functions allow the user to
aggregate data at different geographic levels, which
can be easily specified as "all"
, "ccaa"
,
"prov"
, "mun"
, "mun_district"
,
"sec"
(census tract), and "poll_station"
.
Internally, however, each polling station is geolocated using a set of
codes provided by Spain’s National Statistics Institute (INE). The
extract_code()
function enables us to extract the
code corresponding to the requested level of disaggregation from a full
polling station code.
For example, we can apply the function to polling station B of census
tract "004"
within the first municipal district of the
municipality of Adra ("003"
), located in the province of
Almería ("04"
), which belongs to the autonomous community
of Andalusia ("01"
).
id_INE_poll_station <- "01-04-003-01-004-B"
What would be the municipality code corresponding to the polling station?
extract_code(id_INE_poll_station, level = "mun")
#> [1] "003"
What about the province code?
extract_code(id_INE_poll_station, level = "prov")
#> [1] "04"
What about the municipal district?
extract_code(id_INE_poll_station, level = "mun_district")
#> [1] "01"
By default, it only returns the code at the requested aggregation
level, but if you set full_cod = TRUE
, you can obtain the
full code, including not only the code for that level but also for all
higher levels. For example, every province has a municipality with the
code "001"
, so we need the corresponding province and
autonomous community codes to uniquely identify it.
extract_code(id_INE_poll_station, level = "mun", full_cod = TRUE)
#> [1] "01-04-003"
extract_code(id_INE_poll_station, level = "prov", full_cod = TRUE)
#> [1] "01-04"
extract_code(id_INE_poll_station, level = "mun_district", full_cod = TRUE)
#> [1] "01-04-003-01"
Recoding municipality names
Over the years, various municipal mergers have taken place in Spain,
which means that not all elections feature the same set of
municipalities or the same identifying codes. In order to unify and
standardize the results provided to users, all output tables refer to
the most recent municipality recoding by the Spanish National Statistics
Institute (INE) (latest update: 2023). The helper function
recod_mun()
transparently returns the updated code based on
that recoding, reassigning codes for merged municipalities
accordingly.