Skip to contents

Rankings tables (source = "rank") keep the core column names, but resp is a position: one row per respondent (id), ranking (task) and marked position, resp = 1 for the first choice, as recorded. Two things in the stored table need care before a person-by-item pivot or a Plackett-Luce fit: a skipped position stays a gap (a ballot marked at positions 1 and 3 keeps resp 1 and 3), and a candidate marked twice keeps both rows. Counting rules for both differ by jurisdiction, so the table does not apply one; this function applies the common one.

Usage

irw_rank_compress(df, drop_repeats = TRUE)

Arguments

df

A rankings table, as returned by irw_fetch(name, source = "rank"): columns id, item, resp, and task (taken as a single task when absent).

drop_repeats

Logical. Drop an item's later marks within a ranking? Default TRUE.

Value

A data frame with the same columns as df, resp an integer running 1..k within each id x task.

Details

Within each id x task, rows are taken in resp order. With drop_repeats = TRUE an item that has already been marked at an earlier position is dropped at every later one (most counting rules treat the later mark as a skip). The reserved items (write-in) and (overvote) are never dropped: two write-in rows are usually two different people, and two overvote rows are two separate events. Then resp is renumbered 1..k in rank order, so the gaps close. Every other column is kept as it was. The result is sorted by id, task, resp.

The Python package's irw.rank_compress() does exactly the same; the two are tested on one shared fixture.

See also

irw_fetch, irw_metadata with source = "rank" for each table's design record.

Examples

d <- data.frame(id = c(1, 1, 1, 2), task = 1, item = c("c01", "c03", "c01", "c02"),
                resp = c(1, 3, 4, 2))
irw_rank_compress(d)
#>   id task item resp
#> 1  1    1  c01    1
#> 2  1    1  c03    2
#> 3  2    1  c02    1
irw_rank_compress(d, drop_repeats = FALSE)
#>   id task item resp
#> 1  1    1  c01    1
#> 2  1    1  c03    2
#> 3  1    1  c01    3
#> 4  2    1  c02    1
if (FALSE) { # \dontrun{
d <- irw_fetch("eurovision_2016_final", source = "rank")
first_choices <- subset(irw_rank_compress(d), resp == 1)
} # }