as_ballot_dates: Transform to ballot-date-level observations

as_ballot_datesR Documentation

Transform to ballot-date-level observations

Description

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()

# 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()
data_rdb |> rdb::as_ballot_dates(cols_to_retain = region_custom) |> colnames()

# non-standard columns to retain must actually be retainable
try(
  data_rdb |> rdb::as_ballot_dates(cols_to_retain = title_en)
)

zdaarau/c2d documentation built on Dec. 18, 2024, 1:24 p.m.