R/utilities.R
rowcolval_to_mat.RdColumns 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.
rowcolval_to_mat( .DF, matvals = "matvals", rownames = "rownames", colnames = "colnames", rowtypes = "rowtypes", coltypes = "coltypes", fill = 0 )
| .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 " |
| coltypes | an optional string identifying the types of information found in columns of the matrix to be constructed. Default is " |
| fill | the value for missing entries in the resulting matrix. default is |
a matrix with named rows and columns and, optionally, row and column types
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#> i 1 i 2 #> c 1 11 12 #> c 2 0 22 #> attr(,"rowtype") #> [1] "rowtypes" #> attr(,"coltype") #> [1] "coltypes"#> [1] "rowtypes"#> [1] "coltypes"B <- rowcolval_to_mat(data, rownames = "rows", colnames = "cols", matvals = "vals", rowtypes = "Commodities", coltypes = "Industries") B#> i 1 i 2 #> c 1 11 12 #> c 2 0 22 #> attr(,"rowtype") #> [1] "Commodities" #> attr(,"coltype") #> [1] "Industries"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#> i 1 i 2 #> c 1 11 12 #> c 2 0 22 #> attr(,"rowtype") #> [1] "Commodities" #> attr(,"coltype") #> [1] "Industries"# 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")#> [1] 2data3 <- data.frame(Country = c("GH"), rows = c(NA), cols = c(NA), vals = c(2)) data3 %>% rowcolval_to_mat(rownames = "rows", colnames = "cols", matvals = "vals")#> [1] 2# 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")