expand_to_tidy | R Documentation |
matsindf
data frameAny tidy data frame of matrices (in which each row represents one matrix observation)
can also be represented as a tidy data frame
with each non-zero matrix entry as an observation on its own row.
This function (and collapse_to_matrices()
) convert between the two representations.
expand_to_tidy(
.DF,
matnames = "matnames",
matvals = "matvals",
rownames = "rownames",
colnames = "colnames",
rowtypes = "rowtypes",
coltypes = "coltypes",
drop = NA
)
.DF |
The data frame containing matsindf-style matrices.
( |
matnames |
The name of the column in |
matvals |
The name of the column in |
rownames |
The name for the output column of row names (a string). Default is "rownames". |
colnames |
The name for the output column of column names (a string). Default is "colnames". |
rowtypes |
An optional name for the output column of row types (a string). Default is "rowtypes". |
coltypes |
The optional name for the output column of column types (a string) Default is "coltypes". |
drop |
If specified, the value to be dropped from output,
For example, |
Names for output columns are specified in the rownames
, colnames
,
rowtypes
, and coltypes
, arguments.
The entries of the matsindf-style matrices are stored in an output column named values.
A tidy data frame containing expanded matsindf-style matrices
library(dplyr)
library(matsbyname)
ptype <- "Products"
itype <- "Industries"
tidy <- data.frame(Country = c( "GH", "GH", "GH", "GH", "GH", "GH", "GH",
"US", "US", "US", "US", "GH", "US"),
Year = c( 1971, 1971, 1971, 1971, 1971, 1971, 1971,
1980, 1980, 1980, 1980, 1971, 1980),
matrix = c( "U", "U", "Y", "Y", "Y", "V", "V",
"U", "U", "Y", "Y", "eta", "eta"),
row = c( "c1", "c2", "c1", "c2", "c2", "i1", "i2",
"c1", "c1", "c1", "c2", NA, NA),
col = c( "i1", "i2", "i1", "i2", "i3", "c1", "c2",
"i1", "i2", "i1", "i2", NA, NA),
rowtypes = c( ptype, ptype, ptype, ptype, ptype, itype, itype,
ptype, ptype, ptype, ptype, NA, NA),
coltypes = c(itype, itype, itype, itype, itype, ptype, ptype,
itype, itype, itype, itype, NA, NA),
vals = c(11 , 22, 11 , 22 , 23 , 11 , 22 ,
11 , 12 , 11 , 22, 0.2, 0.3)) %>%
group_by(Country, Year, matrix)
mats <- collapse_to_matrices(tidy, matnames = "matrix", rownames = "row", colnames = "col",
rowtypes = "rowtypes", coltypes = "coltypes",
matvals = "vals") %>%
ungroup()
expand_to_tidy(mats, matnames = "matrix", matvals = "vals",
rownames = "rows", colnames = "cols",
rowtypes = "rt", coltypes = "ct")
expand_to_tidy(mats, matnames = "matrix", matvals = "vals",
rownames = "rows", colnames = "cols",
rowtypes = "rt", coltypes = "ct", drop = 0)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.