matsindf (version 0.3.0)

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 setrowtype and 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)

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.

Value

a matrix with named rows and columns and, optionally, row and column types

Examples

Run this code
# NOT RUN {
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")
# }

Run the code above in your browser using DataCamp Workspace