matsindf (version 0.4.8)

rowcolval_to_mat: Collapse a tidy data frame into a matrix with named rows and columns

Description

Columns not specified in one of rownames, colnames, rowtype, coltype, or values are silently dropped. rowtypes and coltypes are added as attributes to the resulting matrix (via matsbyname::setrowtype() and matsbyname::setcoltype(). The resulting matrix is a (under the hood) a data frame. If both rownames and colnames columns of .DF contain NA, it is assumed that this is a single value, not a matrix, in which case the value in the values column is returned.

Usage

rowcolval_to_mat(
  .DF,
  matvals = "matvals",
  rownames = "rownames",
  colnames = "colnames",
  rowtypes = "rowtypes",
  coltypes = "coltypes",
  fill = 0,
  matrix.class = lifecycle::deprecated(),
  matrix_class = c("matrix", "Matrix"),
  i_colname = "i",
  j_colname = "j"
)

Value

A matrix with named rows and columns and, optionally, row and column types.

Arguments

.DF

A tidy data frame containing columns for row names, column names, and values.

matvals

The name of the column in .DF containing values with which to fill the matrix (a string). Default is "matvals".

rownames

The name of the column in .DF containing row names (a string). Default is "rownames".

colnames

The name of the column in .DF containing column names (a string). Default is "colnames".

rowtypes

An optional string identifying the types of information found in rows of the matrix to be constructed. Default is "rowtypes".

coltypes

An optional string identifying the types of information found in columns of the matrix to be constructed. Default is "coltypes".

fill

The value for missing entries in the resulting matrix. default is 0.

matrix.class

[Deprecated] Use matrix_class instead.

matrix_class

One of "matrix" or "Matrix". "matrix" creates a base::matrix object with the matrix() function. "Matrix" creates a Matrix::Matrix object using the matsbyname::Matrix() function. This could be a sparse matrix. Default is "matrix".

i_colname, j_colname

Names of index columns used internally. Defaults are "i" and "j".

Details

Note that two types of matrices can be created, a matrix or a Matrix. Matrix has the advantage of representing sparse matrices with less memory (and disk space). Matrix objects are created by matsbyname::Matrix().

Examples

Run this code
library(matsbyname)
library(dplyr)
data <- data.frame(Country  = c("GH", "GH", "GH"),
                   rows = c( "c 1",  "c 1", "c 2"),
                   cols = c( "i 1",  "i 2", "i 2"),
                   vals = c(   11  ,   12,    22 ))
A <- rowcolval_to_mat(data, rownames = "rows", colnames = "cols", matvals = "vals")
A
rowtype(A) # NULL, because types not set
coltype(A) # NULL, because types not set
B <- rowcolval_to_mat(data, rownames = "rows", colnames = "cols", matvals = "vals",
                            rowtypes  = "Commodities", coltypes  = "Industries")
B
C <- data %>% bind_cols(data.frame(rt = c("Commodities", "Commodities", "Commodities"),
                                   ct = c("Industries", "Industries", "Industries"))) %>%
  rowcolval_to_mat(rownames = "rows", colnames = "cols", matvals = "vals",
                   rowtypes = "rt", coltypes = "ct")
C
# Also works for single values if both the rownames and colnames columns contain NA
data2 <- data.frame(Country = c("GH"), rows = c(NA), cols = c(NA),
  rowtypes = c(NA), coltypes = c(NA), vals = c(2))
data2 %>% rowcolval_to_mat(rownames = "rows", colnames = "cols", matvals = "vals",
  rowtypes = "rowtypes", coltypes = "coltypes")
data3 <- data.frame(Country = c("GH"), rows = c(NA), cols = c(NA), vals = c(2))
data3 %>% rowcolval_to_mat(rownames = "rows", colnames = "cols", matvals = "vals")
# Fails when rowtypes or coltypes not all same. In data3, column rt is not all same.
data4 <- data %>% bind_cols(data.frame(rt = c("Commodities", "Industries", "Commodities"),
                                       ct = c("Industries", "Industries", "Industries")))
if (FALSE) rowcolval_to_mat(data4, rownames = "rows", colnames = "cols",
                          matvals = "vals", rowtypes = "rt", coltypes = "ct")

Run the code above in your browser using DataLab