Learn R Programming

matsbyname

Statement of need

Matrices are important mathematical objects, and they often describe networks of flows among nodes. Example networks are given in the following table. The power of matrices lies in their ability to organize network-wide calculations, thereby simplifying the work of analysts who study entire systems. However, three problems arise when performing matrix operations in R and other languages. The R package matsbyname enables matrix mathematics wherein operations are performed “by name” and row and column types are allowed.

Problem 1

Although built-in matrix functions ensure size conformity of matrix operands, they do not respect the names of rows and columns (known as dimnames in R). If the rows and columns are not in the same order, mathematical operations with matrices are nonsensical.

Problem 2

In many cases, operand matrices may have different numbers or different names of rows or columns. This situation can occur when, for example, products or industries changes across time periods. When performing matrix operations, rows or columns of zeros must be added to ensure name conformity. The analyst’s burden is cumbersome. But worse problems await. Respecting names (and adding rows and columns of zeroes) can lead to an inability to invert matrices downstream.

Problem 3

Matrix functions provided by R and other languages do not ensure type conformity for matrix operands to matrix algebra functions. In the example of matrix multiplication, columns of the multiplicand must contain the same type of information as the as the rows of the multiplier. If the columns of A are countries, then the rows of B must also be countries (and in the same order) if A %*% B is to make sense.

This package provides functions that respect row and column names when performing matrix mathematics in R. Furthermore, operations can be performed on lists of matrices, such as columns in a matsindf data frame.

Installation

You can install matsbyname from CRAN with:

install.packages("matsbyname")

You can install a recent development version of matsbyname from github with:

# install devtools if not already installed
# install.packages("devtools")
devtools::install_github("MatthewHeun/matsbyname")
# To build vignettes locally, use
devtools::install_github("MatthewHeun/matsbyname", build_vignettes = TRUE)

History

The functions in this package were used in Heun et al. (2018).

More Information

Find more information, including vignettes and function documentation, at https://MatthewHeun.github.io/matsbyname/.

References

Heun, Matthew Kuperus, Anne Owen, and Paul E. Brockway. 2018. “A Physical Supply-Use Table Framework for Energy Analysis on the Energy Conversion Chain.” Applied Energy 226 (September): 1134–62. https://doi.org/10.1016/j.apenergy.2018.05.109.

Copy Link

Version

Install

install.packages('matsbyname')

Monthly Downloads

415

Version

0.6.11

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Matthew Heun

Last Published

January 17th, 2025

Functions in matsbyname (0.6.11)

colprods_byname

Column products, sorted by name
binaryapply_byname

Apply a binary function "by name"
compare_byname

Compare matrix entries to a value
complete_and_sort

Complete matrices relative to one another and sort into same row, column order
coltype

Column type
count_vals_byname

Count the number of matrix entries that meet a criterion
count_vals_incols_byname

Count the number of matrix entries in columns that meet a criterion
clean_byname

Clean (delete) rows or columns of matrices that contain exclusively clean_value
colsums_byname

Column sums, sorted by name
complete_rows_cols

Complete rows and columns in one matrix relative to another
cumprod_byname

Cumulative element-product that respects row and column names
create_colvec_byname

Create column vectors from data
count_vals_inrows_byname

Count the number of matrix entries in rows that meet a criterion
create_matrix_byname

Create a "byname" matrix from a vector
create_rowvec_byname

Create row vectors from data
create_triplet

Create a triplet from a matrix
cumapply_byname

Apply a function cumulatively to a list of matrices or numbers
cumsum_byname

Cumulative sum that respects row and column names
getcolnames_byname

Gets column names
getrownames_byname

Gets row names
elementapply_byname

Apply a function to an element of a matrix specified by rows and columns
equal_byname

Compare two matrices "by name" for equality
geometricmean_byname

Name- and element-wise geometric mean of two matrices.
get_row_col_index_maps

Figure out row and column index maps
.data

Data pronoun
eigenvalues_byname

Calculate eigenvalues of a matrix
hatinv_byname

Hatize and invert a vector
identical_byname

Compare two matrices "by name" for exact equality
fractionize_byname

Compute fractions of matrix entries
exp_byname

Exponential of matrix elements
hatize_byname

Creates a diagonal "hat" matrix from a vector
difference_byname

Name-wise subtraction of matrices
identize_byname

Named identity matrix or vector
eigenvectors_byname

Calculate eigenvectors of a matrix
kvec_from_template_byname

Create a constant vector from matrix a
logmean

Logarithmic mean of two numbers
margin_from_types_byname

Translate row and column types to integer margins
list_of_rows_or_cols

Named list of rows or columns of matrices
invert_byname

Invert a matrix
mean_byname

Name- and element-wise arithmetic mean of matrices
is.Matrix

Is an object a Matrix?
matsbyname-package

matsbyname: An Implementation of Matrix Mathematics that Respects Row and Column Names
getzerorowcolnames_byname

Names of zero rows and columns
hadamardproduct_byname

Name-wise matrix Hadamard multiplication
is_matrix_or_Matrix

Tells whether an object is one of a matrix or a Matrix
%>%

Pipe operator
iszero_byname

Test whether this is the zero matrix
organize_args

Organize binary arguments
naryapply_byname

Apply a function "by name" to any number of operands
log_byname

Logarithm of matrix elements
naryapplylogical_byname

Apply a function logically to numbers, matrices, or lists of numbers or matrices
rowtype

Row type
:=

Assignment
quotient_byname

Name-wise matrix element division
samestructure_byname

Test whether matrices or lists of matrices have same structure
prepare_.FUNdots

Prepare the .FUNdots argument for *apply_byname functions.
prodall_byname

Product of all elements in a matrix
matricize_byname

Matricize a vector
matrixproduct_byname

Name-wise matrix multiplication
replaceNaN_byname

Replace NaN values with a value
rename_to_pref_suff_byname

Rename matrix rows and columns by prefix and suffix
nrow_byname

Get the number of rows in a "byname" matrix.
setcolnames_byname

Sets column names
setcoltype

Sets column type for a matrix or a list of matrices
setrowtype

Sets row type for a matrix or a list of matrices
setrownames_byname

Sets row names
pow_byname

Powers of matrix elements
rename_to_piece_byname

Rename matrix rows and columns by piece of row or column names
select_rowcol_piece_byname

Select or remove rows or columns based on pieces of the names.
reallocate_byname

Reallocate values from one row or column to another
select_cols_byname

Select columns of a matrix (or list of matrices) by name
prep_vector_arg

Prepare a vector argument
sum_byname

Name-wise addition of matrices
selectzerocols_byname

Select zero columns
selectzerorows_byname

Select zero rows
logarithmicmean_byname

Name- and element-wise logarithmic mean of matrices
select_rows_byname

Select (or de-select) rows of a matrix (or list of matrices) by name
sumall_byname

Sum of all elements in a matrix
ncol_byname

Get the number of columns in a "byname" matrix.
rowprods_byname

Row products, sorted by name
svd_byname

Calculate the singular value decomposition of a matrix
switch_notation_byname

Change row and column name notation
to_named_triplet

Convert a matrix or list of matrices between named form and indexed form
vec_from_store_byname

Create a vector with labels from a matrix and values from a vector store
vectorize_byname

Vectorize a matrix
transpose_byname

Transpose a matrix by name
structure_index_map

Set the structure of an index map
sort_rows_cols

Sorts rows and columns of a matrix
trim_rows_cols

Trim rows and/or columns from a matrix
rowsums_byname

Row sums, sorted by name
unaryapply_byname

Apply a unary function by name
select_rows_cols_byname

Select (or deselect) rows or columns
aggregate_pieces_byname

Aggregate a matrix by pieces of row and/or column names
aggregate_to_pref_suff_byname

Aggregate a matrix to prefixes or suffixes of row and/or column names
any_byname

Are any matrix elements TRUE?
aggregation_map_helpers

Aggregation map conversions
aggregate_byname

Aggregate rows and columns in a matrix
Iminus_byname

Subtract a matrix with named rows and columns from a suitably named and sized identity matrix (I)
and_byname

And "by name"
abs_byname

Absolute value of matrix elements
all_byname

Are all matrix elements TRUE?
Matrix

Create a Matrix amenable to use in the matsbyname package