sortit {lazy.tools}R Documentation

Sort an Object according to Keys

Description

Sort an Object according to Keys

Usage

sortit(x, ..., descending = 0, cols = NULL)

Arguments

x

an object to be sorted.
If x is a matrix, all the column will be sorted.

...

voctors to be uses as keys.
Must be the same length as x.

descending

= 1 to sort descendingly when ... is present.

cols

vector consisting of the column numbers of x which will be used as keys.
This has priority over ... .

Details

In order to sort descendingly using cols argument, specify negative column number in cols=
or use - sign in numeric ... ariguments.
Or, use descending=1 option.

Value

Object of the same size as x.

Examples

# sort the rows of x according to the order of ... .
sortit( demomat(4,2), c(4,3,1,2) )
sortit( demomat(4,2), c(4,3,1,2), c("a","a","b","b") )

# sort the rows of x using the colums specified in cols= as keys.
sortit( diag(4), cols=1:4 )

# sorting the rows of a matrix
X <- as.matrix( expand.grid( 1:2, 3:5 ) )
colnames(X)=c("xx1","xx2")
sortit( X, cols=c(1,2) )
Print( sortit( X, cols=c(1,-2) ), sortit( X, X[,1], -X[,2] ) )

# the last column first or the first column first.
Print( sortit( X, cols=1:2 ), sortit( X, cols=2:1 ) )

# sort the result of unique function
set.seed(1701)
Y <- matrix( sample(letters[1:3],99, replace=1),,3 )
Yu <- unique(Y)
Print( Yu, sortit(Yu, cols=1:3) )
Print( sortit(Yu, Yu[, 1], Yu[, 2], Yu[, 3]) )
Print( Yu, sortit(Yu, cols=-(1:3)) )
Print( sortit(Yu, Yu[, 1], Yu[, 2], Yu[, 3], descending=1) )


[Package lazy.tools version 0.1.3 Index]