Transformation from .csv or data.frame (tibble) to prcalc object.

Description

Transformation from .csv or data.frame (tibble) to prcalc object.

Usage

as_prcalc(x, l1, l2 = NULL, p, q = NULL, type = c("nested", "span"), ...)

read_prcalc(x, l1, l2 = NULL, p, q = NULL, type = c("nested", "span"), ...)

Arguments

x a path of .csv file (read_prcalc()), or name of data.frame or tibble object (as_prcalc()).
l1 a character. Mandatory. A column name of level 1 (region or state).
l2 a character. Optional. A column name of level 2 (party or district).
p a character. Mandatory. A column name of population or number of electorates.
q a character. Optional. A column name of magnitude or number of allocated seats. If NULL, replaced by 1.
type a type of l2. Mandatory. If “span”, vector of l2 represents parties. If “nested”, vector of l2 represents electoral district. Default is “nested”.
ignored

Details

At least two columns of x are required—l1 and p.

Value

a prcalc object.

Examples

library(PRcalc)

data("jp_lower_2021_result")

as_prcalc(jp_lower_2021_result,
          l1   = "Pref",
          l2   = "Party",
          p    = "Votes",
          q    = "Seats",
          type = "span")

data("br_district_2010")
# Brazil has single block PR system, l2 can be omitted.
# If type = "party", raw names of districts are preserved.
br_district_2010 |>
  as_prcalc(l1   = "district",
            p    = "population",
            q    = "magnitude",
            type = "span")

data("au_district_2010")
# Because all of magnitude are 1 and type of l2 is district,
# you can omit q and type arguments.
as_prcalc(au_district_2010,
          l1   = "region",
          l2   = "district",
          p    = "electorates")
# You can import .csv format and transform into prcalc object directly.
read_prcalc("data/my_file.csv",
            l1   = "region",
            l2   = "district",
            p    = "electorates",
            q    = "magnitude",
            type = "nested")