matrixStats icon indicating copy to clipboard operation
matrixStats copied to clipboard

WISH: rowRanks(x, ties.method='first') wanted

Open arrayn opened this issue 9 years ago • 3 comments

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.

arrayn avatar May 13 '15 18:05 arrayn

Just some notes for the future:

From help("rank", package="base"):

If all components are different (and no NAs), the ranks are well defined, with values in seq_along(x). With some values equal (called ‘ties’), the argument ties.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)))))

HenrikBengtsson avatar May 13 '15 20:05 HenrikBengtsson

Adding my vote to this wish. An inefficient implementation for "first" is

x[] = sort.list(sort.list(x))
rowRanks(x)

mtmorgan avatar Jun 03 '18 10:06 mtmorgan

FYI - The new version (0.55.0) provides all ties.method options, see: https://github.com/HenrikBengtsson/matrixStats/pull/146

cgiachalis avatar Sep 12 '19 08:09 cgiachalis