By default, the matsbyname
package expends matrices
with 0
rows or columns prior to matrix operations
to ensure that rows and columns match.
There are times when trimming rows or columns is preferred
over the default behavior.
This function trims rows or columns in a
to match
the rows or columns of mat
.
The return value will have rows or columns of a
removed if they do not appear in mat
.
trim_rows_cols(a = NULL, mat = NULL, margin = c(1, 2))
a | A matrix to be trimmed. |
---|---|
mat | The matrix |
margin | The dimension of |
Matrix a
with rows or columns trimmed to match mat
.
If a
is NULL
, NULL
is returned.
If mat
is NULL
, a
is returned unmodified.
If mat
has NULL
dimnames, a
is returned unmodified.
If mat
has NULL
for dimnames on margin
, an error is returned.
a <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE, dimnames = list(c("r1", "r2", "r3"), c("c1", "c2", "c3"))) %>% setrowtype("rowtype") %>% setcoltype("coltype") mat <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE, dimnames = list(c("r1", "bogus"), c("c1", "bogus", "c2"))) %>% setrowtype("rowtype") %>% setcoltype("coltype") trim_rows_cols(a, mat, margin = 1)#> c1 c2 c3 #> r1 1 2 3 #> attr(,"rowtype") #> [1] "rowtype" #> attr(,"coltype") #> [1] "coltype"trim_rows_cols(a, mat, margin = 2)#> c1 c2 #> r1 1 2 #> r2 4 5 #> r3 7 8 #> attr(,"rowtype") #> [1] "rowtype" #> attr(,"coltype") #> [1] "coltype"trim_rows_cols(a, mat)#> c1 c2 #> r1 1 2 #> attr(,"rowtype") #> [1] "rowtype" #> attr(,"coltype") #> [1] "coltype"