matrixStats
matrixStats copied to clipboard
Feature request: whichRowMaxs, whichColMaxs, whichRowMins, and whichColMins
This is a request for adding whichRowMaxs, whichColMaxs, whichRowMins, whichColMins to matrixStats.
Arguments would be the same as rowMaxs and family (colMaxs, rowMins, colMins) but instead of returning the max or min values, the which* functions would return the indices of the max or min values.
More precisely, in the case of whichRowMaxs, the function would return an integer vector with one element per row in the input matrix. Each value in this vector would be the column index of the max in the corresponding row of the input matrix. In case of ties, the lowest index would be returned. If there is no max, either because the input matrix has zero column, or na.rm=TRUE and all the values in a row are missing (NAs or NaNs), or na.rm=FALSE and at least one value in the row is missing, then the index of the max would be considered to be NA.
Example:
m <- rbind(A=c(11, 7, 15),
B=c(-2, 9, 9),
C=c(9L, NaN, 0L),
D=c(NA, NaN, NA))
Expected results:
whichRowMaxs(m)
[1] 3 2 NA NA
whichRowMaxs(m, na.rm=TRUE)
[1] 3 2 1 NA
whichRowMaxs(m[ , 0])
[1] NA NA NA NA
Here is a naive implementation of whichRowMaxs() that implements the semantic described above (leaving the rows, cols, and dim. arguments aside):
naive_whichRowMaxs <- function(x, na.rm=FALSE)
{
ans <- apply(x, 1,
function(row) {
j <- which.max(row)
if (length(j) == 0L) NA_integer_ else j
})
if (!na.rm)
ans[rowAnyMissings(x)] <- NA_integer_
ans
}
Thanks!