[R] Sorting vector based on pairs of comparisons

Richard M. Heiberger rmh @end|ng |rom temp|e@edu
Thu Mar 14 13:04:04 CET 2019


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 set.

On Thu, Mar 14, 2019 at 07:30 Pedro Conte de Barros <pbarros using ualg.pt> wrote:

> 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")
>
> Larger <- c("SDR", "EDF", "KLM", "KLM", "SDR", "EDF"
>
> matComp <- cbind(Smaller, Larger)
>
> so that matComp looks like this
>
>       Smaller Larger
> [1,] "ASD"   "SDR"
> [2,] "DFE"   "EDF"
> [3,] "ASD"   "KLM"
> [4,] "SDR"   "KLM"
> [5,] "EDF"   "SDR"
> [6,] "ASD"   "EDF"
>
> This matrix establishes six pairs of "larger than" relationships that
> can be used to sort the unique values in the matrix,
>
>  > unique(as.vector(matComp))
> [1] "ASD" "DFE" "SDR" "EDF" "KLM"
>
> Specifically, I would like to get this:
>
> sorted <- c("ASD", "DFE", "EDF", "SDR", "KLM")
>
> or, equally valid (my matrix does not have the full information):
>
> sorted <- c("DFE", "ASD", "EDF", "SDR", "KLM")
>
> Preferably, I would get the different combinations of the unique values
> that satisfy the "larger than" conditions in the matrix...
>
>
> I am sure this is a trivial problem, but I could not find any algorithm
> to solve it.
>
> Any help would be highly appreciated
>
> ______________________________________________
> R-help using 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list