Skip to contents

Transforms referendum-level observations to ones on the level of ballot date and jurisdiction via nesting of referendum-level columns. The individual values of all the referendums on a specific ballot date in a specific jurisdiction are preserved in a list column named rfrnd_data.

Usage

as_ballot_dates(data, cols_to_retain = NULL)

Arguments

data

RDB referendum data as returned by rfrnds(). A data frame that at minimum contains the column date.

cols_to_retain

Additional non-standard columns to be preserved as top-level columns instead of being nested in the list column rfrnd_data. They mustn't vary within ballot-date-level observations. Tidy selections are supported.

Value

A tibble.

See also

Other referendum data transformation functions: n_rfrnds(), n_rfrnds_per_period(), prettify_col_names(), unnest_var()

Examples

# standard RDB columns are retained as far as possible
rdb::rfrnds(quiet = TRUE) |>
  rdb::as_ballot_dates()
#> # A tibble: 5,938 × 8
#>    country_code country_name  subnational_entity_name municipality level       date       is_former_country rfrnd_data       
#>    <fct>        <fct>         <chr>                   <chr>        <ord>       <date>     <lgl>             <list>           
#>  1 CH           Switzerland   Zug                     NA           subnational 2024-06-09 NA                <tibble [1 × 62]>
#>  2 LI           Liechtenstein NA                      NA           national    2024-09-22 NA                <tibble [1 × 62]>
#>  3 CH           Switzerland   Jura                    NA           subnational 2024-09-22 NA                <tibble [1 × 62]>
#>  4 CH           Switzerland   Bern                    NA           subnational 2024-09-22 NA                <tibble [2 × 62]>
#>  5 CH           Switzerland   Basel Landschaft        NA           subnational 2024-09-22 NA                <tibble [1 × 62]>
#>  6 CH           Switzerland   Graubünden              NA           subnational 2024-09-22 NA                <tibble [1 × 62]>
#>  7 CH           Switzerland   Schwyz                  NA           subnational 2024-09-22 NA                <tibble [1 × 62]>
#>  8 CH           Switzerland   Solothurn               NA           subnational 2024-09-22 NA                <tibble [2 × 62]>
#>  9 CH           Switzerland   Zug                     NA           subnational 2024-09-22 NA                <tibble [2 × 62]>
#> 10 CH           Switzerland   Schaffhausen            NA           subnational 2024-08-18 NA                <tibble [2 × 62]>
#> # ℹ 5,928 more rows

# non-standard columns must be explicitly specified in order to be retained
data_rdb <-
  rdb::rfrnds(quiet = TRUE) |>
    rdb::add_world_regions() |>
    dplyr::mutate(region_custom =
                    factor(x = dplyr::if_else(country_code == "CH",
                                              "Switzerland & Liechtenstein",
                                              un_region_tier_1_name),
                           levels = c("Switzerland & Liechtenstein",
                                      levels(un_region_tier_1_name))) |>
                    forcats::fct_relevel("Switzerland & Liechtenstein",
                                         after = 3L) |>
                    forcats::fct_recode("rest of Europe" = "Europe"))

data_rdb |> rdb::as_ballot_dates() |> colnames()
#>  [1] "country_code"            "country_name"            "subnational_entity_name" "municipality"            "un_country_code"         "un_region_tier_1_code"  
#>  [7] "un_region_tier_1_name"   "un_region_tier_2_code"   "un_region_tier_2_name"   "un_region_tier_3_code"   "un_region_tier_3_name"   "un_subregion"           
#> [13] "level"                   "date"                    "is_former_country"       "rfrnd_data"             
data_rdb |> rdb::as_ballot_dates(cols_to_retain = region_custom) |> colnames()
#>  [1] "country_code"            "country_name"            "subnational_entity_name" "municipality"            "un_country_code"         "un_region_tier_1_code"  
#>  [7] "un_region_tier_1_name"   "un_region_tier_2_code"   "un_region_tier_2_name"   "un_region_tier_3_code"   "un_region_tier_3_name"   "un_subregion"           
#> [13] "level"                   "date"                    "is_former_country"       "region_custom"           "rfrnd_data"             

# non-standard columns to retain must actually be retainable
try(
  data_rdb |> rdb::as_ballot_dates(cols_to_retain = title_en)
)
#> Error in rdb::as_ballot_dates(data_rdb, cols_to_retain = title_en) : 
#>   Retaining the additional non-standard column `title_en` while converting to ballot-date-level observations is impossible because this column varies
#> within ballot dates.