Learn R Programming

crank (version 1.1-2)

spsort: Simple partial sorting of a vector of elements.

Description

Sort the elements in a vector according to a set of precedence rules.

Usage

spsort(x,L=NULL)

Arguments

x

A matrix or data frame with at least two columns. The first two columns are interpreted as precedence pairs, meaning that the element in column 1 should appear before the one in column 2.

L

The vector of elements to be sorted. If NULL, it becomes all of the unique elements in x[1:2,].

Value

The vector L sorted by the rules in x.

Details

spsort steps through rows of x identifying the positions of the leading and trailing elements in each rule. If the leading element in the rule does not precede the trailing element in L, its position in L is moved to just ahead of the trailing element. If all of the possible precedence rules for the vector L are specified, the sorting will be unique. In most cases, the order of the result will depend upon the initial order of L and the order of the precendence rules.

Examples

Run this code
# NOT RUN {
 # Pedro's example
 Smaller<-c("ASD", "DFE", "ASD", "SDR", "EDF", "ASD")
 Larger<-c("SDR", "EDF", "KLM", "KLM", "SDR", "EDF")
 matComp<-cbind(Smaller,Larger)
 spsort(matComp)
 # scramble the order of rules
 nmatrows<-nrow(matComp)
 spsort(matComp[sample(1:nmatrows,nmatrows),])
 # David Urbina's example
 priors<-c("A","B","C","C","D","E","E","F","G")
 posts<-c("E","H","A","D","E","B","F","G","H")
 dinnerMat<-cbind(priors,posts)
 spsort(dinnerMat)
 # add the condition that the taquitos must precede the guacamole
 dinnerMat<-rbind(dinnerMat,c("G","B"))
 spsort(dinnerMat)
 # scramble the rows
 nmatrows<-nrow(dinnerMat)
 spsort(dinnerMat[sample(1:nmatrows,nmatrows),])
# }

Run the code above in your browser using DataLab