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
.
Arguments
- data
RDB referendum data as returned by
rfrnds()
. A data frame that at minimum contains the columndate
.- 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_per_period()
,
n_rfrnds()
,
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,884 × 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 PL Poland NA NA national 2023-10-15 NA <tibble [4 × 62]>
#> 2 AU Australia NA NA national 2023-10-14 NA <tibble [1 × 62]>
#> 3 EC Ecuador NA NA national 2023-08-20 NA <tibble [1 × 62]>
#> 4 FM Micronesia NA NA national 2023-07-04 NA <tibble [8 × 62]>
#> 5 CF Central African Republic NA NA national 2023-07-30 NA <tibble [1 × 62]>
#> 6 ML Mali NA NA national 2023-06-18 NA <tibble [1 × 62]>
#> 7 CH Switzerland Vaud NA subnational 2023-06-18 NA <tibble [1 × 62]>
#> 8 CH Switzerland Fribourg NA subnational 2023-06-18 NA <tibble [1 × 62]>
#> 9 CH Switzerland Schaffhausen NA subnational 2023-06-18 NA <tibble [1 × 62]>
#> 10 CH Switzerland Appenzell Ausserrhoden NA subnational 2018-09-23 NA <tibble [2 × 62]>
#> # ℹ 5,874 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.