mixedOrder | R Documentation |
order alphanumeric values keeping numeric values in proper order
mixedOrder(
x,
...,
blanksFirst = TRUE,
na.last = NAlast,
keepNegative = FALSE,
keepInfinite = FALSE,
keepDecimal = FALSE,
ignore.case = TRUE,
useCaseTiebreak = TRUE,
honorFactor = FALSE,
returnDebug = FALSE,
returnType = c("order", "rank"),
NAlast = TRUE,
verbose = FALSE,
debug = FALSE
)
x |
input vector |
... |
additional parameters are sent to |
blanksFirst |
|
na.last |
|
keepNegative |
|
keepInfinite |
|
keepDecimal |
|
ignore.case |
|
useCaseTiebreak |
|
honorFactor |
|
returnDebug |
|
returnType |
|
NAlast |
|
verbose |
|
debug |
|
This function is a refactor of gtools::mixedorder()
which was
the source of inspiration for this function, thanks to Gregory R. Warnes!
This function was designed to improve the efficiency for large vectors,
and to handle special cases slightly differently. It was driven by some
need to sort gene symbols, and miRNA symbols in numeric order, for example:
miR-12,miR-1,miR-122,miR-1b,miR-1a,miR-2
sort
:miR-1,miR-12,miR-122,miR-1a,miR-1b,miR-2
gtools::mixedsort
:miR-122,miR-12,miR-2,miR-1,miR-1a,miR-1b
mixedSort
:miR-1,miR-1a,miR-1b,miR-2,miR-12,miR-122
This function does not by default consider negative numbers as negative, instead it treats '-' as a delimiter, unless keepNegative=TRUE.
When keepNegative=TRUE
this function also recognizes scientific
notation, for example "1.23e-2"
will be treated as numeric 0.0123
.
Note that keepNegative=TRUE
also forces keepDecimal=TRUE
.
When keepDecimal=TRUE
this function maintains numeric values that
include one "."
.
This function is the core of a family of mixedSort functions:
mixedSort()
Applies mixedOrder()
to an input vector.
mixedSorts()
Applies mixedOrder()
to a list of vectors,
returning the list where each vector is independently sorted.
mixedSortDF()
Applies mixedOrder()
to each column of a
data.frame
or comparable object, optionally specifying the order
of columns used during the sort.
Extra thanks to Gregory R. Warnes for the gtools::mixedorder()
that proved to be so useful it ultimately inspired this function.
integer
vector of orders derived from x,
or when returnType="rank"
an integer vector of ranks allowing ties.
The rank is therefore valid for use in chains, such as multiple
columns of a data.frame
.
gtools::mixedorder()
, gtools::mixedsort()
Other jam sort functions:
mixedSortDF()
,
mixedSorts()
,
mixedSort()
,
mmixedOrder()
Other jam string functions:
asSize()
,
breaksByVector()
,
cPasteSU()
,
cPasteS()
,
cPasteUnique()
,
cPasteU()
,
cPaste()
,
fillBlanks()
,
formatInt()
,
gsubOrdered()
,
gsubs()
,
makeNames()
,
mixedSortDF()
,
mixedSorts()
,
mixedSort()
,
mmixedOrder()
,
nameVectorN()
,
nameVector()
,
padInteger()
,
padString()
,
pasteByRowOrdered()
,
pasteByRow()
,
sizeAsNum()
,
tcount()
,
ucfirst()
,
uniques()
x <- c("miR-12","miR-1","miR-122","miR-1b", "miR-1a","miR-2");
mixedOrder(x);
x[mixedOrder(x)];
mixedSort(x);
order(x);
x[order(x)];
sort(x);
## Complex example including NA, blanks, and infinite "Inf"
x <- c("Inf",
"+Inf12",
NA,
"-Inf14",
"-",
"---",
"Jnf12",
"Hnf12",
"--",
"Information");
## By default, strings are sorted as-is, "Hnf" before "Inf" before "Jnf"
## blanks are first, NA values are last
x[mixedOrder(x)];
## blanks are last, but before NA values which are also last
x[mixedOrder(x, blanksFirst=FALSE)];
## Recognize infinite, but not the negative sign
## Now infinite values are at the end, ordered by the number that follows.
x[mixedOrder(x, blanksFirst=FALSE, keepInfinite=TRUE)]
## Now also recognize negative infinite values,
## which puts "-Inf14" at the very beginning.
x[mixedOrder(x, blanksFirst=FALSE, keepInfinite=TRUE, keepNegative=TRUE)]
# test factor level order
factor1 <- factor(c("Cnot9", "Cnot8", "Cnot10"))
sort(factor1)
mixedSort(factor1)
factor1[mixedOrder(factor1)]
factor1[mixedOrder(factor1, honorFactor=TRUE)]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.