Learn R Programming

futile.matrix (version 1.2.7)

select: Select a portion of a matrix based on a regular expression of the row and/or column names.

Description

Extract 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
# NOT RUN {
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