trim_rows_cols: Trim rows and/or columns from a matrix

Description Usage Arguments Details Value Examples

View source: R/trim.R

Description

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.

Usage

1
trim_rows_cols(a = NULL, mat = NULL, margin = c(1, 2))

Arguments

a

A matrix to be trimmed.

mat

The matrix

margin

The dimension of a to be trimmed. 1 means rows; 2 means columns. Default is c(1,2).

Details

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.

Value

Matrix a with rows or columns trimmed to match mat.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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)
trim_rows_cols(a, mat, margin = 2)
trim_rows_cols(a, mat)

matsbyname documentation built on Oct. 13, 2021, 1:06 a.m.