# sparse.extract.index: Sparse indexing of matrices In xrobin/xavamess: Useful Miscellaneous Functions

## Description

Computes a matrix index that extracts individual (i, j) elements. This differ from the standard extraction where all columns in rows `i` and all rows in columns `j` are extracted.

## Usage

 ```1 2``` ```sparse.extract.index(i, j, nrow, ncol, byrow = FALSE, na.rm = FALSE, quiet = TRUE) ```

## Arguments

 `i, j` the indices of the elements to extract. `i` and `j` must have the same length. `nrow, ncol` the number of rows and colums of the index. currently, `ncol` is ignored with `byrow = FALSE`, `nrow` is ignored with `byrow = TRUE` `byrow` only `FALSE` is supported for now `na.rm` remove elements where i or j are `NA` `quiet` if `FALSE`, prints a `message` before removing the missing values. If `TRUE`, missing values are silently removed.

## Details

Note that this is likely to NOT work on data frames.

## Value

the vector index for the matrix. Typically one would want to use this index to extract from a matrix, and a vector will be returned, regardless of the status of `drop`.

## Examples

 ```1 2 3 4 5``` ```m <- matrix(1:25, 5, 5) m[1:5, 1:5] # Returns the whole matrix m[sparse.extract.index(1:5, 1:5, nrow = 5)] # Returns the diagonal only m[c(1, 3, 5), c(2, 4, 4)] m[sparse.extract.index(c(1, 3, 5), c(2, 4, 4), nrow = 5)] ```

xrobin/xavamess documentation built on May 13, 2018, 12:15 p.m.