HenrikBengtsson / matrixStats

R package: Methods that Apply to Rows and Columns of Matrices (and to Vectors)
https://cran.r-project.org/package=matrixStats
203 stars 33 forks source link

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

Closed arrayn closed 6 months ago

arrayn commented 9 years ago

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.

HenrikBengtsson commented 9 years ago

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)))))
mtmorgan commented 6 years ago

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

x[] = sort.list(sort.list(x))
rowRanks(x)
cgiachalis commented 5 years ago

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