| mixedSortDF | R Documentation |
sort data.frame keeping numeric values in proper order
mixedSortDF(
df,
byCols = seq_len(ncol(df)),
na.last = TRUE,
decreasing = NULL,
useRownames = FALSE,
verbose = FALSE,
blanksFirst = TRUE,
keepNegative = FALSE,
keepInfinite = FALSE,
keepDecimal = FALSE,
ignore.case = TRUE,
useCaseTiebreak = TRUE,
sortByName = FALSE,
honorFactor = TRUE,
...
)
df |
|
byCols |
one of two types of input:
|
na.last |
|
decreasing |
NULL or |
useRownames |
|
verbose |
|
blanksFirst, keepNegative, keepInfinite, keepDecimal, ignore.case, useCaseTiebreak, sortByName |
arguments passed to |
honorFactor |
|
... |
additional arguments passed to |
This function is a wrapper around mmixedOrder() so it operates
on data.frame columns in the proper order, using logic similar that used
by base::order() when operating on a data.frame. The sort order logic
is fully described in mixedSort() and mixedOrder().
Note that byCols can either be given as integer column index values,
or character vector of colnames(x). In either case, using negative
prefix - will reverse the sort order of the corresponding column.
For example byCols=c(2, -1) will sort column 2 increasing, then
column 1 decreasing.
Similarly, one can supply colnames(df), such as
byCols=c("colname2", "-colname1"). Values are matched as-is to
colnames(df) first, then any values not matched are compared again
after removing prefix - from the start of each character string.
Therefore, if colnames(df) contains "-colname1" it will be matched
as-is, but "--colname1" will only be matched after removing the first -,
after which the sort order will be reversed for that column.
For direct control over the sort order of each column defined in byCols,
you can supply logical vector to argument decreasing, and this vector
is recycled to length(byCols).
Finally, for slight efficiency, only unique columns defined in byCols
are used to determine the row order, so even if a column is defined twice
in byCols, only the first instance is passed to mmixedOrder() to
determine row order.
data.frame whose rows are ordered using mmixedOrder().
Other jam sort functions:
mixedOrder(),
mixedSort(),
mixedSorts(),
mmixedOrder()
# start with a vector of miRNA names
x <- c("miR-12","miR-1","miR-122","miR-1b", "miR-1a","miR-2");
# add some arbitrary group information
g <- rep(c("Air", "Treatment", "Control"), 2);
# create a data.frame
df <- data.frame(group=g,
miRNA=x,
stringsAsFactors=FALSE);
# input data
df;
# output when using order()
df[do.call(order, df), , drop=FALSE];
# output with mixedSortDF()
mixedSortDF(df);
# mixedSort respects factor order
# reorder factor levels to demonstrate.
# "Control" should come first
gf <- factor(g, levels=c("Control", "Air", "Treatment"));
df2 <- data.frame(groupfactor=gf,
miRNA=x,
stringsAsFactors=FALSE);
# now the sort properly keeps the group factor levels in order,
# which also sorting the miRNA names in their proper order.
mixedSortDF(df2);
x <- data.frame(l1=letters[1:10],
l2=rep(letters[1:2+10], 5),
L1=LETTERS[1:10],
L2=rep(LETTERS[1:2+20], each=5));
set.seed(123);
rownames(x) <- sample(seq_len(10));
x;
# sort by including rownames
mixedSortDF(x, byCols=c("rownames"));
mixedSortDF(x, byCols=c("L2", "-rownames"));
# demonstrate sorting a matrix with no rownames
m <- matrix(c(2, 1, 3, 4), ncol=2);
mixedSortDF(m, byCols=-2)
# add rownames
rownames(m) <- c("c", "a");
mixedSortDF(m, byCols=0)
mixedSortDF(m, byCols="-rownames")
mixedSortDF(m, byCols="rownames")
mixedSortDF(data.frame(factor1=factor(c("Cnot9", "Cnot8", "Cnot10"))), honorFactor=FALSE)
# test date columns
testfiles <- system.file(package="jamba", c("TODO.md", "README.md", "NEWS.md"))
testinfo <- file.info(testfiles)
testinfo
mixedSortDF(testinfo, byCols="mtime")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.