select: Select a portion of a matrix based on a regular expression of...

Description Usage Arguments Details Value Examples

View source: R/manip.R

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

1
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

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.

Value

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

Examples

1
2
3
4
5
6
7
8
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"))

Example output

            Fertility Agriculture Examination Education Catholic
Rive Droite      44.7        46.6          16        29    50.43
Rive Gauche      42.8        27.7          22        29    58.33
            Infant.Mortality
Rive Droite             18.2
Rive Gauche             19.3
             Examination Education
Courtelary            15        12
Delemont               6         9
Franches-Mnt           5         5
Moutier               12         7
Neuveville            17        15
Porrentruy             9         7
Broye                 16         7
Glane                 14         8
Gruyere               12         7
Sarine                16        13
Veveyse               14         6
Aigle                 21        12
Aubonne               14         7
Avenches              19        12
Cossonay              22         5
Echallens             18         2
Grandson              17         8
Lausanne              26        28
La Vallee             31        20
Lavaux                19         9
Morges                22        10
Moudon                14         3
Nyone                 22        12
Orbe                  20         6
Oron                  12         1
Payerne               14         8
Paysd'enhaut           6         3
Rolle                 16        10
Vevey                 25        19
Yverdon               15         8
Conthey                3         2
Entremont              7         6
Herens                 5         2
Martigwy              12         6
Monthey                7         3
St Maurice             9         9
Sierre                 3         3
Sion                  13        13
Boudry                26        12
La Chauxdfnd          29        11
Le Locle              22        13
Neuchatel             35        32
Val de Ruz            15         7
ValdeTravers          25         7
V. De Geneve          37        53
Rive Droite           16        29
Rive Gauche           22        29
             Examination Education
Courtelary            15        12
Delemont               6         9
Franches-Mnt           5         5
Moutier               12         7
Neuveville            17        15
Porrentruy             9         7
Broye                 16         7
Glane                 14         8
Gruyere               12         7
Sarine                16        13
Aigle                 21        12
Aubonne               14         7
Avenches              19        12
Cossonay              22         5
Echallens             18         2
Grandson              17         8
Lausanne              26        28
La Vallee             31        20
Lavaux                19         9
Morges                22        10
Moudon                14         3
Nyone                 22        12
Orbe                  20         6
Oron                  12         1
Payerne               14         8
Paysd'enhaut           6         3
Rolle                 16        10
Conthey                3         2
Entremont              7         6
Herens                 5         2
Martigwy              12         6
Monthey                7         3
St Maurice             9         9
Sierre                 3         3
Sion                  13        13
Boudry                26        12
La Chauxdfnd          29        11
Le Locle              22        13
Neuchatel             35        32
Rive Droite           16        29
Rive Gauche           22        29

futile.matrix documentation built on May 2, 2019, 4:22 a.m.