Learn R Programming

prefio (version 0.2.0)

ranking_matrix: Compute the Rankings Matrix for a vector of preferences

Description

Convert a set of preferences to a rankings matrix, where each preference defines a single row in the output. The columns in the rankings matrix give the vector or ranks assigned to the corresponding candidate.

Usage

ranking_matrix(x, preferences_col = NULL, frequency_col = NULL, ...)

Value

An \(N\) by \(M\) matrix, where \(N\) is the number of preferences, and \(M\) is the number of items.

Arguments

x

A preferences object or a tibble with a preferences-typed column.

preferences_col

<tidy-select> When x is a tibble, the column containing the preferences.

frequency_col

<tidy-select> When x is a tibble, the column containing the frequency of the preferences. If not provided, each row is considered to be observed a single time.

...

Currently unused.

Details

For a preferences vector of length \(N\) with \(M\) items, the rankings matrix is an \(N\) by \(M\) matrix, with element \((i, j)\) being the rank assigned to candidate \(j\) in the \(i\)th selection.

Examples

Run this code
x <- tibble::tribble(
  ~voter_id, ~species, ~food, ~ranking,
  1, "Rabbit", "Apple", 1,
  1, "Rabbit", "Banana", 2,
  1, "Rabbit", "Carrot", 3,
  2, "Monkey", "Banana", 1,
  2, "Monkey", "Apple", 2,
  2, "Monkey", "Carrot", 3
) |>
  long_preferences(
    food_preference,
    id_cols = voter_id,
    item_col = food,
    rank_col = ranking
  ) |>
  dplyr::pull(food_preference) |>
  ranking_matrix()

Run the code above in your browser using DataLab