order2 {lazy.tools}R Documentation

Returns a permutation which rearranges its first argument in the order specified by the second argument.

Description

Returns a permutation which rearranges its first argument in the order specified by the second argument.

Usage

order2(x, order = NULL, descending = 0)

Arguments

x

a vector to be sorted according to order

order

a vector containing the definition of the sort order
If order is not given, this function returns the same as the native order.

descending

= 1 to sort descendingly

Details

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)]

Value

The permutation vector, od, to be used as:
x_sorted <- x[od]

Examples

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] )


[Package lazy.tools version 0.1.4 ]