mat_to_rowcolval: Convert a matrix to a data frame with rows, columns, and...

Description Usage Arguments Value Examples

View source: R/utilities.R

Description

This function "expands" a matrix into a tidy data frame with a values column and factors for row names, column names, row types, and column types. Optionally, values can be dropped.

Usage

1
2
3
mat_to_rowcolval(.matrix, matvals = "matvals", rownames = "rownames",
  colnames = "colnames", rowtypes = "rowtypes",
  coltypes = "coltypes", drop = NA)

Arguments

.matrix

the IO-style matrix to be converted to a data frame with rows, columns, and values

matvals

a string for the name of the output column containing values. Default is "matvals".

rownames

a string for the name of the output column containing row names. Default is "rownames".

colnames

a string for the name of the output column containing column names. Default is "colnames".

rowtypes

a string for the name of the output column containing row types. Default is "rowtypes".

coltypes

a string for the name of the output column containing column types. Default is "coltypes".

drop

if specified, the value to be dropped from output. Default is NA. For example, drop = 0 will cause 0 entries in the matrices to be deleted from output. If NA, no values are dropped from output.

Value

a data frame with rows, columns, and values

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
library(magrittr)
library(matsbyname)
data <- data.frame(Country  = c("GH", "GH", "GH"),
                   rows = c( "c1",  "c1", "c2"),
                   cols = c( "i1",  "i2", "i2"),
                   rt = c("Commodities", "Commodities", "Commodities"),
                   ct = c("Industries", "Industries", "Industries"),
                   vals = c(  11  ,  12,   22 ))
data
A <- data %>%
  rowcolval_to_mat(rownames = "rows", colnames = "cols",
                   rowtypes = "rt",   coltypes = "ct", matvals = "vals")
A
mat_to_rowcolval(A, rownames = "rows", colnames = "cols",
                 rowtypes = "rt", coltypes = "ct", matvals = "vals")
mat_to_rowcolval(A, rownames = "rows", colnames = "cols",
                 rowtypes = "rt", coltypes = "ct", matvals = "vals", drop = 0)
# This also works for single values
mat_to_rowcolval(2, matvals = "vals",
                 rownames = "rows", colnames = "cols",
                 rowtypes = "rt", coltypes = "ct")
mat_to_rowcolval(0, matvals = "vals",
                 rownames = "rows", colnames = "cols",
                 rowtypes = "rt", coltypes = "ct", drop = 0)

MatthewHeun/matsindf documentation built on Nov. 12, 2018, 2:32 p.m.