Import and preprocess surveys data for given election types and dates. This function supports both single values and vector inputs for fetching and combining data for multiple elections at once.
Usage
import_survey_data(
type_elec = "congress",
year = NULL,
date = NULL,
verbose = TRUE,
format = "long",
forthcoming = TRUE
)
Arguments
- type_elec
Type elections for which data is available. It should be one of the following values: "referendum", "congress", "senate", "local", "cabildo" (Canarian council) or "EU".
- year
A vector or single value representing the years of the elections to be considered. Please, check in
dates_elections_spain
that elections of the specified type are available for the provided year.- date
A vector or single value representing the dates of the elections to be considered. If date was provided, it should be in format %Y-%m-%d (e.g., '2000-01-01'). Defaults to
NULL
. If no date was provided,year
should be provided as numerical variable. Please, check indates_elections_spain
that elections of the specified type are available.- verbose
Flag to indicate whether detailed messages should be printed during execution. Defaults to
TRUE
.- format
Do you want the output in long or wide format? Defaults to
"long"
- forthcoming
A flag indicates whether user wants to include surveys for the forthcoming elections. Defaults to
TRUE
. IfTRUE
, no date neither year are required (in that case, just surveys for the next elections are provided).
Value
A tibble with rows corresponding to the estimated results for each party for each election given by a particular pollster, including the following variables:
- id_survey
survey's id constructed from the polling firm and the dates for the start and end of the fieldwork.
- id_elec
election's id constructed from the election code and date.
- polling_firm
organisation conducting the poll.
- media
commissioning organisation / media outlet.
- fieldwork_start, fieldwork_end, n_field_days
fieldwork period and its length (days).
- sample_size
sample size of the survey.
- abbrev_candidacies, name_candidacies_nat
acronym and full name of the candidacies.
- id_candidacies_nat
id for candidacies at national level.
- estimated_porc_ballots
estimated percentage of ballots for each party.
Details
This function fetches survey data for the
specified elections' date by loading the corresponding files from
{pollspaindata}
and processing them by joining the ids and name
of the different political parties.
Examples
## Correct examples
# Fetch survey data for congress elections in multiple dates
poll_station_data <-
import_survey_data(year = 2023, date = "2019-04-28")
#> Import survey data
#> [x] Checking if parameters are allowed...
#> [x] Importing survey data ...
# ----
# Incorrect examples
# ----
if (FALSE) { # \dontrun{
# Wrong examples
# Invalid election type: "national" is not a valid election type
import_survey_data(type_elec = "national", year = 2019)
# Invalid election: no congress elections are available in 2018
# Please check dataset dates_elections_spain
import_survey_data(type_elec = "congress", year = 2018)
# Invalid date format: date should be in %Y-%m-%d format
import_survey_data(date = "26-06-2016")
} # }