Re: [R] Sorting vector based on pairs of comparisons

2019-03-15 Thread Jim Lemon
Hi Bert, Good reference and David Urbina's example showed that a simple swap was position dependent. The reason I pursued this is that it seems more efficient to sequentially apply the precedence rules to the arbitrarily sorted elements of the vector than to go through the directed graph approach.

Re: [R] Sorting vector based on pairs of comparisons

2019-03-15 Thread Pedro Conte de Barros
Thanks Bert. Excellent reference, I learned a lot from it! Just a note: I did use search engines for at least 2 days before posting. BUT as often happens, I did not use the right keywords. I tried several variants of "Convert ordered pairs to sorted", "Sort vector on paired comparisons" and

Re: [R] Sorting vector based on pairs of comparisons

2019-03-15 Thread Bert Gunter
If I understand correctly, the answer is a topological sort. Here is an explanation https://davidurbina.blog/on-partial-order-total-order-and-the-topological-sort/ This was found by a simple web search on "Convert partial ordering to total ordering" Btw. Please use search engines before

Re: [R] Sorting vector based on pairs of comparisons

2019-03-14 Thread Jim Lemon
Hi Pedro, This looks too simple to me, but it seems to work: swap<-function(x,i1,i2) { tmp<-x[i1] x[i1]<-x[i2] x[i2]<-tmp return(x) } mpo<-function(x) { L<-unique(as.vector(x)) for(i in 1:nrow(x)) { i1<-which(L==x[i,1]) i2<-which(L==x[i,2]) if(i2 wrote: > > Dear All, > > This should

Re: [R] Sorting vector based on pairs of comparisons

2019-03-14 Thread William Dunlap via R-help
This is called topological sorting in some circles. The function below will give you one ordering that is consistent with the contraints but not all possible orderings. I couldn't find such a function in core R so I wrote one a while back based on Kahn's algorithm, as described in Wikipedia. >

Re: [R] Sorting vector based on pairs of comparisons

2019-03-14 Thread Pedro Conte de Barros
Thanks for this. Yes, this is checked before trying to process this. Pedro On 14/03/2019 14.09, Bert Gunter wrote: This cannot be done unless transitivity is guaranteed. Is it? S L a b b c c a Bert On Thu, Mar 14, 2019, 4:30 AM Pedro Conte de Barros mailto:pbar...@ualg.pt>> wrote:

Re: [R] Sorting vector based on pairs of comparisons

2019-03-14 Thread Bert Gunter
This cannot be done unless transitivity is guaranteed. Is it? S L a b b c c a Bert On Thu, Mar 14, 2019, 4:30 AM Pedro Conte de Barros wrote: > Dear All, > > This should be a quite established algorithm, but I have been searching > for a couple days already without finding any

Re: [R] Sorting vector based on pairs of comparisons

2019-03-14 Thread Richard M. Heiberger
Try this. Anything that appears only in Smaller is candidate for smallest. Among those, order is arbitrary. Anything that appears only in Larger is a candidate for largest. Among those order is arbitrary. Remove rows of matComp containing the already classified items. Repeat with the smaller

[R] Sorting vector based on pairs of comparisons

2019-03-14 Thread Pedro Conte de Barros
Dear All, This should be a quite established algorithm, but I have been searching for a couple days already without finding any satisfactory solution. I have a matrix defining pairs of Smaller-Larger arbitrary character values, like below Smaller <- c("ASD", "DFE", "ASD", "SDR", "EDF", "ASD")