colProds,xgCMatrix-method {sparseMatrixStats} | R Documentation

## Calculates the product for each row (column) in a matrix

### Description

Calculates the product for each row (column) in a matrix

### Usage

    ## S4 method for signature 'xgCMatrix'
    colProds(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = NA)

    ## S4 method for signature 'xgCMatrix'
    rowProds(x, rows = NULL, cols = NULL, na.rm = FALSE, 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.
`na.rm` | If `TRUE`, `NA`s are excluded first, otherwise not.
`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

Attention: This method ignores the order of the values, because it assumes that the product is commutative. Unfortunately, for 'double' this is not true. For example 'NaN * NA = NaN', but 'NA * NaN = NA'. This is relevant for this function if there are '+-Inf', because 'Inf * 0 = NaN'. This function returns 'NA' whenever there is 'NA' in the input. This is different from 'matrixStats::colProds()'.

### Value

Returns a `numeric` `vector` of length N (K).

### See Also

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

- For sums across rows (columns), see `rowSums2()` (`colSums2()`)

- `base::prod()`.

### Examples

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

      print(mat)

      rowProds(mat)
      colProds(mat)

[Package sparseMatrixStats version 1.6.0 Index]