Description Usage Arguments Details Value See Also Examples

A "tidy" data frame contains information that can be collapsed into matrices,
including columns for
matrix names, row names, column names, row types, column types, and values (entries in matrices).
These column names are specified as strings by the `matnames`

, `rownames`

, `colnames`

,
`rowtypes`

, `coltypes`

, and `values`

arguments to collapse_to_matrices, respectively.
A matsindf-style matrix has named rows and columns.
In addition, matsindf-style matrices have "types" for row and column information,
such as "Commodities", "Industries", "Products", or "Machines".
The row and column types for the matsindf-style matrices are stored as attributes on the matrix
(`rowtype`

and `coltype`

),
which can be accessed with the `rowtype`

and `coltype`

functions
of the matsbyname package.
Row and column types are both respected and propagated by the various `_byname`

functions
of the matsbyname package.
Use the `*_byname`

functions when you do operations on the matsindf-style matrices.
The matsindf-style matrices will be stored
in a column with same name as the incoming `values`

column.
This function is similar to `nest`

, which stores data frames into a cell of a data frame.
With `collapse_to_matrices`

, matrices are created.
This function is similar to `summarise`

in that groups are respected.
(In fact, calls to this function may not work properly unless grouping is provided.
Errors of the form "Error: Duplicate identifiers for rows ..." are usually fixed by
grouping `.DF`

prior to calling this function.)
The usual approach is to `group_by`

the `matnames`

column
and any other columns to be preserved in the output.
Note that execution is halted if any of
`rownames`

, `rowtypes`

, `colnames`

, `coltypes`

, or `values`

is a grouping variable.
`rowtypes`

and `coltypes`

should be the same for all rows of the same matrix;
execution is halted if that is not the case.
`spread`

ing the output by `matnames`

may be necessary before calculations are done on the matrices.
See the example.

1 2 3 | ```
collapse_to_matrices(.DF, matnames = "matnames", matvals = "matvals",
rownames = "rownames", colnames = "colnames",
rowtypes = "rowtypes", coltypes = "coltypes")
``` |

`.DF` |
the "tidy" data frame |

`matnames` |
a string identifying the column in |

`matvals` |
a string identifying the column in |

`rownames` |
a string identifying the column in |

`colnames` |
a string identifying the column in |

`rowtypes` |
optional string identifying the column in |

`coltypes` |
optional string identifying the column in |

Groups are not preserved on output.

a data frame with matrices in columns

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | ```
library(magrittr)
library(dplyr)
library(tidyr)
library(tibble)
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", "E", "E", "E", "V", "V",
"U", "U", "E", "E", "eta", "eta"),
row = c( "c 1", "c 2", "c 1", "c 2", "c 2", "i 1", "i 2",
"c 1", "c 1", "c 1", "c 2", NA, NA),
col = c( "i 1", "i 2", "i 1", "i 2", "i 3", "c 1", "c 2",
"i 1", "i 2", "i 1", "i 2", 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", matvals = "vals",
rownames = "row", colnames = "col",
rowtypes = "rowtypes", coltypes = "coltypes")
mats %>% spread(key = matrix, value = vals)
``` |

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

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.