maxn {cgwtools} | R Documentation |
Functions to find the n-th maximum or minimum of a vector or array.
Description
These functions behave similarly to min, max
and which(x == max/min (x)
to find the n-th greatest max or min of a dataset.
Usage
maxn(x,nth=1)
minn(x,nth=1)
which.maxn(x, nth = 1, arr.ind = FALSE, useNames = TRUE)
which.minn(x, nth = 1, arr.ind = FALSE, useNames = TRUE)
Arguments
x |
A vector or array of numerical or string values. Note: ordering of string values may not be what is expected. See Details. |
nth |
Which lesser(greater, for min functions) to find, with default nth==1 being identical to |
arr.ind |
Same meaning as for |
useNames |
Same meaning as for |
Details
Quoting the help page for max
:
Character versions are sorted lexicographically, and this depends on the collating sequence of the locale in use: the help for Comparison gives details. The max/min of an empty character vector is defined to be character NA. (One could argue that as "" is the smallest character element, the maximum should be "", but there is no obvious candidate for the minimum.)
Value
For maxn, minn
, a single value which is the nth- max or min.
For the which.min,max
functions, quoting from which
:
If arr.ind == FALSE (the default), an integer vector, or a double vector if x is a long vector, with length equal to sum(x), i.e., to the number of TRUEs in x.
If arr.ind == TRUE and x is an array (has a dim attribute), the result is arrayInd(which(x), dim(x), dimnames(x)), namely a matrix whose rows each are the indices of one element of x
Author(s)
Carl Witthoft, carl@witthoft.com
See Also
Examples
set.seed(17) # for repeatability
foo <- matrix(sample(1:10,20,replace=TRUE),5,4)
maxn(foo,3)
which.minn(foo,4)