R/Utilities.R
select_cols_byname.Rd
Arguments indicate which columns are to be retained and which are to be removed. For maximum flexibility, arguments are extended regex patterns that are matched against column names.
select_cols_byname(a, retain_pattern = "$^", remove_pattern = "$^")
a | a matrix or a list of matrices |
---|---|
retain_pattern | an extended regex or list of extended regular expressions that specifies which columns of |
remove_pattern | an extended regex or list of extended regular expressions that specifies which columns of |
a matrix that is a subset of a
with columns selected by retain_pattern
and remove_pattern
.
If a
is NULL
, NULL
is returned.
Patterns are compared against column names using extended regex.
If no column names of a
match the retain_pattern
, NULL
is returned.
If no column names of a
match the remove_pattern
, a
is returned.
Retaining columns takes precedence over removing columns, always.
Some typical patterns are:
^Electricity$|^Oil$
: column names that are EXACTLY Electricity
or Oil
.
^Electricity|^Oil
: column names that START WITH Electricity
or Oil
.
Electricity|Oil
: column names that CONTAIN Electricity
or Oil
anywhere within them.
Given a list of column names, a pattern can be constructed easily using the make_pattern
function.
make_pattern
escapes regex strings using escapeRegex
.
This function assumes that retain_pattern
and remove_pattern
have already been
suitably escaped.
Note that the default retain_pattern
and remove_pattern
($^
)
retain nothing and remove nothing.
Note that if all columns are removed from a
, NULL
is returned.
m <- matrix(1:16, ncol = 4, dimnames=list(c(paste0("i", 1:4)), paste0("p", 1:4))) %>% setrowtype("Industries") %>% setcoltype("Commodities") select_cols_byname(m, retain_pattern = make_pattern(c("p1", "p4"), pattern_type = "exact"))#> p1 p4 #> i1 1 13 #> i2 2 14 #> i3 3 15 #> i4 4 16 #> attr(,"rowtype") #> [1] "Industries" #> attr(,"coltype") #> [1] "Commodities"#> p2 p4 #> i1 5 13 #> i2 6 14 #> i3 7 15 #> i4 8 16 #> attr(,"rowtype") #> [1] "Industries" #> attr(,"coltype") #> [1] "Commodities"#> [[1]] #> p1 p4 #> i1 1 13 #> i2 2 14 #> i3 3 15 #> i4 4 16 #> attr(,"rowtype") #> [1] "Industries" #> attr(,"coltype") #> [1] "Commodities" #> #> [[2]] #> p1 p4 #> i1 1 13 #> i2 2 14 #> i3 3 15 #> i4 4 16 #> attr(,"rowtype") #> [1] "Industries" #> attr(,"coltype") #> [1] "Commodities" #>