order2 {lazy.tools} | R Documentation |
Returns a permutation which rearranges its first argument in the order specified by the second argument.
order2(x, order = NULL, descending = 0)
x |
a vector to be sorted according to order |
order |
a vector containing the definition of the sort order |
descending |
= 1 to sort descendingly |
The returning value, od, will be calculated as:
df1 <- data.frame( id1=1:length(x), x )
df2 <- data.frame( id2=1:length(order), order )
temp <- merge( df1, df2, by.x="x", by.y="order", all=1 )
temp <- temp[order(temp$id2),]
od <- (temp$id1)[1:length(x)]
The permutation vector, od
, to be used as:
x_sorted <- x[od]
order <- c("B", "D", "A", "C", "E","X")
x <- c( "A", "D", "B", "E", "C", "D", "A" )
od <- order2( x, order )
Print( x, order, od, x[od] )