[R] "unfurling" rankings into a matrix of preferences

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Mon Jul 27 21:38:22 CEST 2015


I have 5 items in total (1:5), but I show a person only 4 items (1:4)
and ask this person to rank items 1:4 in terms of preferences (1 is
best, 2 is second best, 4 is worst), and I get a vector of ranks:
ranks <- c(2,4,3,1)

# That means that this person liked item 4 best and item 2 worst.

I would like to "unfirl" this vector of ranks into a matrix of
preferences where if the row item prefers the column item, then it's a
1. Otherwise, it's a zero. So, the output should be a 5 by 5 matrix
(because overall we have 5 items, not 4, but item 5 did not
participate in rankings), and it would always have zeros in a
diagonal.:

0    1    1    0 NA
0    0    0    0 NA
0    1    0    0 NA
1    1    1    0 NA
NA NA NA NA 0

I can loop through all possible pairs the person saw and fill the
matrix accordingly, but it seems like a lot of looping. Could one do
it in a more elegant way?

Thank you very much!


-- 
Dimitri Liakhovitski



More information about the R-help mailing list