Dimitri Liakhovitski
2015-Jul-27 21:31 UTC
[R] "unfurling" rankings into a matrix of preferences
Wow! On Mon, Jul 27, 2015 at 5:28 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote:> ## I leave it to you to add the NA edges > >> rk <- c(2,4,3,1) > >> outer(rk,rk,"<")+0 > > [,1] [,2] [,3] [,4] > [1,] 0 1 1 0 > [2,] 0 0 0 0 > [3,] 0 1 0 0 > [4,] 1 1 1 0 > > > > Cheers, > Bert > > Bert Gunter > > "Data is not information. Information is not knowledge. And knowledge > is certainly not wisdom." > -- Clifford Stoll > > > On Mon, Jul 27, 2015 at 12:38 PM, Dimitri Liakhovitski > <dimitri.liakhovitski at gmail.com> wrote: >> 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 >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code.-- Dimitri Liakhovitski
Dimitri Liakhovitski
2015-Jul-27 21:32 UTC
[R] "unfurling" rankings into a matrix of preferences
With NAs it'd be: rk <- c(2,NA,4,3,1, NA) outer(rk, rk, "<") + 0 Wow, I still can't believe it - just one line! On Mon, Jul 27, 2015 at 5:31 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:> Wow! > > On Mon, Jul 27, 2015 at 5:28 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote: >> ## I leave it to you to add the NA edges >> >>> rk <- c(2,4,3,1) >> >>> outer(rk,rk,"<")+0 >> >> [,1] [,2] [,3] [,4] >> [1,] 0 1 1 0 >> [2,] 0 0 0 0 >> [3,] 0 1 0 0 >> [4,] 1 1 1 0 >> >> >> >> Cheers, >> Bert >> >> Bert Gunter >> >> "Data is not information. Information is not knowledge. And knowledge >> is certainly not wisdom." >> -- Clifford Stoll >> >> >> On Mon, Jul 27, 2015 at 12:38 PM, Dimitri Liakhovitski >> <dimitri.liakhovitski at gmail.com> wrote: >>> 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 >>> >>> ______________________________________________ >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. > > > > -- > Dimitri Liakhovitski-- Dimitri Liakhovitski
No it wouldn't. Presumably you have a typo and meant rk <- c(2,4,3,1,NA) ## and set the (5,5) entry to 0 after -- Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Mon, Jul 27, 2015 at 2:32 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:> With NAs it'd be: > > rk <- c(2,NA,4,3,1, NA) > outer(rk, rk, "<") + 0 > > Wow, I still can't believe it - just one line! > > > On Mon, Jul 27, 2015 at 5:31 PM, Dimitri Liakhovitski > <dimitri.liakhovitski at gmail.com> wrote: >> Wow! >> >> On Mon, Jul 27, 2015 at 5:28 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote: >>> ## I leave it to you to add the NA edges >>> >>>> rk <- c(2,4,3,1) >>> >>>> outer(rk,rk,"<")+0 >>> >>> [,1] [,2] [,3] [,4] >>> [1,] 0 1 1 0 >>> [2,] 0 0 0 0 >>> [3,] 0 1 0 0 >>> [4,] 1 1 1 0 >>> >>> >>> >>> Cheers, >>> Bert >>> >>> Bert Gunter >>> >>> "Data is not information. Information is not knowledge. And knowledge >>> is certainly not wisdom." >>> -- Clifford Stoll >>> >>> >>> On Mon, Jul 27, 2015 at 12:38 PM, Dimitri Liakhovitski >>> <dimitri.liakhovitski at gmail.com> wrote: >>>> 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 >>>> >>>> ______________________________________________ >>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >>>> and provide commented, minimal, self-contained, reproducible code. >> >> >> >> -- >> Dimitri Liakhovitski > > > > -- > Dimitri Liakhovitski