Learn R Programming

⚠️There's a newer version (0.6.11) of this package.Take me there.

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

560

Version

0.6.8

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Matthew Heun

Last Published

December 21st, 2023

Functions in matsbyname (0.6.8)

cumapply_byname

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

Name-wise subtraction of matrices
create_matrix_byname

Create a "byname" matrix from a vector
create_rowvec_byname

Create row vectors from data
eigenvalues_byname

Calculate eigenvalues of a matrix
hadamardproduct_byname

Name-wise matrix Hadamard multiplication
getzerorowcolnames_byname

Names of zero rows and columns
compare_byname

Compare matrix entries to a value
getcolnames_byname

Gets column names
getrownames_byname

Gets row names
cumsum_byname

Cumulative sum that respects row and column names
.data

Data pronoun
hatinv_byname

Hatize and invert a vector
is_matrix_or_Matrix

Tells whether an object is one of a matrix or a Matrix
iszero_byname

Test whether this is the zero matrix
hatize_byname

Creates a diagonal "hat" matrix from a vector
eigenvectors_byname

Calculate eigenvectors of a matrix
cumprod_byname

Cumulative element-product that respects row and column names
matsbyname-package

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

Named identity matrix or vector
exp_byname

Exponential of matrix elements
identical_byname

Compare two matrices "by name" for exact equality
mean_byname

Name- and element-wise arithmetic mean of matrices
equal_byname

Compare two matrices "by name" for equality
nrow_byname

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

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

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

Logarithmic mean of two numbers
ncol_byname

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

Organize binary arguments
margin_from_types_byname

Translate row and column types to integer margins
prodall_byname

Product of all elements in a matrix
prepare_.FUNdots

Prepare the .FUNdots argument for *apply_byname functions.
count_vals_inrows_byname

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

Matricize a vector
create_colvec_byname

Create column vectors from data
kvec_from_template_byname

Create a constant vector from matrix a
elementapply_byname

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

Named list of rows or columns of matrices
samestructure_byname

Test whether matrices or lists of matrices have same structure
select_cols_byname

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

Pipe operator
fractionize_byname

Compute fractions of matrix entries
invert_byname

Invert a matrix
is.Matrix

Is an object a Matrix?
geometricmean_byname

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

Name-wise matrix multiplication
rename_to_piece_byname

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

Sum of all elements in a matrix
select_rows_byname

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

Calculate the singular value decomposition of a matrix
select_rowcol_piece_byname

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

Replace NaN values with a value
rowprods_byname

Row products, sorted by name
sort_rows_cols

Sorts rows and columns of a matrix
sum_byname

Name-wise addition of matrices
setcolnames_byname

Sets column names
rename_to_pref_suff_byname

Rename matrix rows and columns by prefix and suffix
vec_from_store_byname

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

Transpose a matrix by name
setcoltype

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

Powers of matrix elements
switch_notation_byname

Change row and column name notation
selectzerorows_byname

Select zero rows
selectzerocols_byname

Select zero columns
unaryapply_byname

Apply a unary function by name
trim_rows_cols

Trim rows and/or columns from a matrix
prep_vector_arg

Prepare a vector argument
vectorize_byname

Vectorize a matrix
quotient_byname

Name-wise matrix element division
logarithmicmean_byname

Name- and element-wise logarithmic mean of matrices
rowsums_byname

Row sums, sorted by name
:=

Assignment
rowtype

Row type
setrowtype

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

Logarithm of matrix elements
setrownames_byname

Sets row names
Matrix

Create a Matrix amenable to use in the matsbyname package
any_byname

Are any matrix elements TRUE?
Iminus_byname

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

Are all matrix elements TRUE?
abs_byname

Absolute value of matrix elements
aggregation_map_helpers

Aggregation map conversions
and_byname

And "by name"
aggregate_to_pref_suff_byname

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

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

Aggregate rows and columns in a matrix
complete_and_sort

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

Count the number of matrix entries that meet a criterion
colprods_byname

Column products, sorted by name
colsums_byname

Column sums, sorted by name
coltype

Column type
binaryapply_byname

Apply a binary function "by name"
clean_byname

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

Complete rows and columns in one matrix relative to another
count_vals_incols_byname

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