Compute the Rankings Matrix for a vector of preferences
Source:R/ranking_matrix.R
ranking_matrix.Rd
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.
Arguments
- x
A
preferences
object or atibble
with apreferences
-typed column.- preferences_col
<
tidy-select
> Whenx
is atibble
, the column containing the preferences.- frequency_col
<
tidy-select
> Whenx
is atibble
, the column containing the frequency of the preferences. If not provided, each row is considered to be observed a single time.- ...
Currently unused.
Value
An \(N\) by \(M\) matrix, where \(N\) is the number of preferences, and \(M\) is the number of items.
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
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()