colRanks,dgCMatrix-method {sparseMatrixStats} | R Documentation

## Calculates the rank of the elements for each row (column) of a matrix-like object

### Description

Calculates the rank of the elements for each row (column) of a matrix-like object.

### Usage

    ## S4 method for signature 'dgCMatrix'
    colRanks(
      x,
      rows = NULL,
      cols = NULL,
      ties.method = c("max", "average", "min"),
      preserveShape = FALSE,
      na.handling = c("keep", "last"),
      ...,
      useNames = NA
    )

    ## S4 method for signature 'dgCMatrix'
    rowRanks(
      x,
      rows = NULL,
      cols = NULL,
      ties.method = c("max", "average", "min"),
      preserveShape = TRUE,
      na.handling = c("keep", "last"),
      ...,
      useNames = NA
    )

### Arguments

`x` | An NxK matrix-like object.
`rows` | A `vector` indicating the subset of rows (and/or columns) to operate over. If `NULL`, no subsetting is done.
`cols` | A `vector` indicating the subset of rows (and/or columns) to operate over. If `NULL`, no subsetting is done.
`ties.method` | A character string specifying how ties are treated. Note that the default specifies fewer options than the original matrixStats package.
`preserveShape` | a boolean that specifies if the returned matrix has the same dimensions as the input matrix. By default this is true for 'rowRanks()', but false for 'colRanks()'.
`na.handling` | string specifying how 'NA's are handled. They can either be preserved with an 'NA' rank ('keep') or sorted in at the end ('last'). Default is 'keep' derived from the behavior of the equivalent
`...` | Additional arguments passed to specific methods.
`useNames` | If `NA`, the default behavior of the function about naming support is remained. If `FALSE`, no naming support is done. Else if `TRUE`, names attributes of result are set.

### Details

There are three different methods available for handling ties:

'max'

  for values with identical values the maximum rank is returned

'average'

  for values with identical values the average of the ranks they cover is returned. Note, that in this case the return value is of type 'numeric'.

'min'

  for values with identical values the minimum rank is returned.

### Value

a matrix of type `integer` is returned unless `ties.method = "average"`. Ithas dimensions' `NxJ` (`KxJ`) `matrix`, where N (K) is the number of rows (columns) of the input x.

### See Also

- `matrixStats::rowRanks()` and `matrixStats::colRanks()` which are used when the input is a `matrix` or `numeric` vector.

- base::rank

### Examples

    mat <- matrix(rnorm(15), nrow = 5, ncol = 3)
      mat[2, 1] <- NA
      mat[3, 3] <- Inf
      mat[4, 1] <- 0

      print(mat)

      rowRanks(mat)
      colRanks(mat)

[Package sparseMatrixStats version 1.6.0 Index]