Learn R Programming

futile.matrix (version 1.2.2)

select: Select a subset of a matrix and make assignments as necessary

Description

'select' extracts a subset of a matrix based on regex patterns on either the rownames, the colnames or both. Once this subset has been selected, assignments can be made following standard consistency rules.

Usage

select(m, row.pat = NULL, col.pat = NULL, ...)

Arguments

m
A matrix from which to select a subset
row.pat
A regular expression to use for rownames
col.pat
A regular expression to use for colnames
...
Additional arguments to pass to grep

Value

A matrix containing all rows and columns that satisfy the patterns given. If no values match, then an empty matrix will be returned.

Details

Oftentimes it is useful to get at a specific subset of data within a matrix. In large matrices, it can be cumbersome to access specific rows and/or columns using traditional subsetting methods, particularly if it is a complex set that is to be extracted. 'select' provides regex searching on named matrices to access portions of a matrix that satisfy the regex. Note that 'select' will work for data.frames as well.

It is possible to assign values to the selected subset as a means to modify the original matrix. Standard consistency rules must be satisfied for any assignment operations.

Examples

Run this code
  library(datasets)
  select(swiss, "Rive")

  select(swiss, col.pat="E", fixed=TRUE)

  select(swiss, row.pat='^[A-T]', col.pat="^E")

  select(swiss, "Rive", "Ed") <- min(select(swiss, "^[^R]", "Ed"))

Run the code above in your browser using DataLab