matrixStats
matrixStats copied to clipboard
WISH: rowRanks(x, ties.method='first') wanted
Thank you for a really useful R package matrixStats_0.14.0. I noticed that matrixStats::rowRanks method lacks a ties.method='first' option, which is available in base::rank. Actually trying this results in error:
rowRanks(x, ties.method='first') Error in if (tiesMethod == 0L) { : missing value where TRUE/FALSE needed
Just so that you know that some people would like to have that option: We have use case in a production system, where we need exactly ties.method='first' semantics. That is, there is exactly one column (first) with rank=1, exactly one column (first) with rank=2 and so on.
Thank you for your time.
Just some notes for the future:
From help("rank", package="base")
:
If all components are different (and no
NA
s), the ranks are well defined, with values inseq_along(x)
. With some values equal (called ‘ties’), the argumentties.method
determines the result at the corresponding indices. The"first"
method results in a permutation with increasing values at each index set of ties. The"random"
method puts these in random order whereas the default,"average"
, replaces them by their mean, and"max"
and"min"
replaces them by their maximum and minimum respectively, the latter being the typical sports ranking.
and from the source code src/library/base/R/rank.R:
y <- switch(ties.method,
"average" = , "min" = , "max" =
.Internal(rank(x, length(x), ties.method)),
"first" = sort.list(sort.list(x)),
"random" = sort.list(order(x, stats::runif(sum(!nas)))))
Adding my vote to this wish. An inefficient implementation for "first" is
x[] = sort.list(sort.list(x))
rowRanks(x)
FYI - The new version (0.55.0) provides all ties.method
options, see: https://github.com/HenrikBengtsson/matrixStats/pull/146