| rowcolval_to_mat {matsindf} | R Documentation |
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"
)
Arguments
.DF |
A tidy data frame containing columns for row names, column names, and values. |
matvals |
The name of the column in |
rownames |
The name of the column in |
colnames |
The name of the column in |
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 |
matrix.class |
|
matrix_class |
One of "matrix" or "Matrix".
"matrix" creates a |
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().
Value
A matrix with named rows and columns and, optionally, row and column types.
Examples
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")))
## Not run: rowcolval_to_mat(data4, rownames = "rows", colnames = "cols",
matvals = "vals", rowtypes = "rt", coltypes = "ct")
## End(Not run)