colProds {DelayedMatrixStats} | R Documentation

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

### Description

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

### Usage

    colProds(x, rows = NULL, cols = NULL, na.rm = FALSE,
      method = c("direct", "expSumLog"), ...)

    rowProds(x, rows = NULL, cols = NULL, na.rm = FALSE,
      method = c("direct", "expSumLog"), ...)

    ## S4 method for signature 'DelayedMatrix'
    colProds(x, rows = NULL, cols = NULL,
      na.rm = FALSE, method = c("direct", "expSumLog"),
      force_block_processing = FALSE, ...)

    ## S4 method for signature 'SolidRleArraySeed'
    colProds(x, rows = NULL, cols = NULL,
      na.rm = FALSE, method = c("direct", "expSumLog"), ...)

    ## S4 method for signature 'DelayedMatrix'
    rowProds(x, rows = NULL, cols = NULL,
      na.rm = FALSE, method = c("direct", "expSumLog"),
      force_block_processing = FALSE, ...)

### Arguments

`x` | A NxK DelayedMatrix.
`rows` | A `vector` indicating subset of elements (or rows and/or columns) to operate over. If `NULL`, no subsetting is done.
`cols` | A `vector` indicating subset of elements (or rows and/or columns) to operate over. If `NULL`, no subsetting is done.
`na.rm` | If `TRUE`, missing values are ignored, otherwise not.
`method` | A `character` string specifying how each product is calculated.
`...` | Additional arguments passed to specific methods.
`force_block_processing` | `FALSE` (the default) means that a seed-aware, optimised method is used (if available). This can be overridden to use the general block-processing strategy by setting this to `TRUE` (typically not advised). The block-processing strategy loads one or more (depending on `getOption("DelayedArray.block.size")`) columns (`colFoo()`) or rows (`rowFoo()`) into memory as an ordinary base::array.

### Details

If `method = "expSumLog"`, then then `product`() function is used, which calculates the produce via the logarithmic transform (treating negative values specially). This improves the precision and lowers the risk for numeric overflow. If `method = "direct"`, the direct product is calculated via the `prod`() function.

### Value

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

### Missing values

Note, if `method = "expSumLog"`, `na.rm = FALSE`, and `x` contains missing values (`NA` or `NaN`), then the calculated value is also missing value. Note that it depends on platform whether `NaN` or `NA` is returned when an `NaN` exists, cf. `is.nan`().

### Examples

    # A DelayedMatrix with a 'matrix' seed
    dm_matrix <- DelayedArray(matrix(c(rep(1L, 5),
                                       as.integer((0:4) ^ 2),
                                       seq(-5L, -1L, 1L)),
                                     ncol = 3))
    # A DelayedMatrix with a 'HDF5ArraySeed' seed
    # NOTE: Requires that the HDF5Array package is installed
    library(HDF5Array)
    dm_HDF5 <- writeHDF5Array(matrix(c(rep(1L, 5),
                                       as.integer((0:4) ^ 2),
                                       seq(-5L, -1L, 1L)),
                                     ncol = 3))

    colProds(dm_matrix)

    rowProds(dm_matrix)

[Package DelayedMatrixStats version 1.0.3 Index]