Skip to contents

Counts the number of RDB referendums per desired period, optionally by additional columns specified via by.

Usage

n_rfrnds_per_period(
  data,
  by = NULL,
  period = c("week", "month", "quarter", "year", "decade", "century"),
  fill_gaps = TRUE,
  period_floor = NULL,
  period_ceiling = NULL,
  descending = FALSE
)

Arguments

data

RDB referendum data as returned by rfrnds(). A data frame that at minimum contains the column specified in period or the column date (to compute the period column), plus the one(s) specified via by (if any).

by

Optional data column(s) to group by before counting number of referendums. Tidy selections are supported.

period

Type of period to count referendums by. One of "week", "month", "quarter", "year", "decade" or "century".

fill_gaps

Whether or not to add zero-value rows to the result for period gaps in data.

period_floor

Lower period limit up to which gaps are filled. If NULL, the lower limit is set to the minimum of period present in data. Only relevant if fill_gaps = TRUE and period is set to a unique timespan type ("year", "decade" or "century").

period_ceiling

Upper period limit up to which gaps are filled. If NULL, the upper limit is set to the maximum of period present in data. Only relevant if fill_gaps = TRUE and period is set to a unique timespan type ("year", "decade" or "century").

descending

Whether to sort the resulting table by period in descending or in ascending order.

Value

A tibble.

Details

Note that the period types "year", "decade" and "century" are unique timespans, while "week", "month" and "quarter" are recurring timespans (e.g. every year has a week 1).

See also

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

Examples

rdb::rfrnds(quiet = TRUE) |>
  rdb::n_rfrnds_per_period()
#> # A tibble: 53 × 2
#>     week     n
#>    <int> <int>
#>  1     1  2774
#>  2     2    57
#>  3     3    59
#>  4     4    57
#>  5     5   135
#>  6     6   222
#>  7     7    94
#>  8     8   195
#>  9     9   422
#> 10    10   355
#> # ℹ 43 more rows

rdb::rfrnds(quiet = TRUE) |>
  rdb::n_rfrnds_per_period(by = level)
#> # A tibble: 159 × 3
#>    level        week     n
#>    <ord>       <int> <int>
#>  1 local           1     1
#>  2 subnational     1  2758
#>  3 national        1    15
#>  4 local           2     0
#>  5 subnational     2    15
#>  6 national        2    42
#>  7 local           3     0
#>  8 subnational     3    34
#>  9 national        3    25
#> 10 local           4     0
#> # ℹ 149 more rows

# without filling gaps
rdb::rfrnds(quiet = TRUE) |>
  rdb::n_rfrnds_per_period(by = level,
                           fill_gaps = FALSE)
#> # A tibble: 122 × 3
#>    level        week     n
#>    <ord>       <int> <int>
#>  1 local           1     1
#>  2 subnational     1  2758
#>  3 national        1    15
#>  4 subnational     2    15
#>  5 national        2    42
#>  6 subnational     3    34
#>  7 national        3    25
#>  8 subnational     4    25
#>  9 national        4    32
#> 10 local           5     2
#> # ℹ 112 more rows

# per decade and by multiple columns
rdb::rfrnds(quiet = TRUE) |>
  rdb::n_rfrnds_per_period(by = c(level, type),
                           period = "decade")
#> # A tibble: 576 × 4
#>    level       type                    decade     n
#>    <ord>       <fct>                    <int> <int>
#>  1 local       mandatory referendum      1790     0
#>  2 local       optional referendum       1790     0
#>  3 local       counter proposal          1790     0
#>  4 local       citizens' initiative      1790     0
#>  5 local       governmental referendum   1790     1
#>  6 local       citizens' assembly        1790     0
#>  7 local       recall                    1790     0
#>  8 local       NA                        1790     0
#>  9 subnational mandatory referendum      1790     0
#> 10 subnational optional referendum       1790     3
#> # ℹ 566 more rows

# count ballot dates instead of referendums
rdb::rfrnds(quiet = TRUE) |>
  rdb::as_ballot_dates() |>
  rdb::n_rfrnds_per_period()
#> # A tibble: 53 × 2
#>     week     n
#>    <int> <int>
#>  1     1   614
#>  2     2    16
#>  3     3    39
#>  4     4    33
#>  5     5    67
#>  6     6   124
#>  7     7    57
#>  8     8   106
#>  9     9   217
#> 10    10   148
#> # ℹ 43 more rows