Skip to content

Commit

Permalink
Merge branch 'release-0.2.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewHeun committed Nov 8, 2018
2 parents 62f1064 + bdf2756 commit c836370
Show file tree
Hide file tree
Showing 35 changed files with 811 additions and 784 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: matsindf
Type: Package
Title: Work with matrices in data frames
Version: 0.2.9
Date: 2018-11-06
Version: 0.2.10
Date: 2018-11-08
Authors@R: c(person("Matthew", "Heun", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-7438-214X"),
email = "[email protected]"))
Expand All @@ -13,7 +13,7 @@ Description: Provides functions to collapse a tidy data frame into matrices in a
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.0
RoxygenNote: 6.1.1
Depends: R (>= 2.10)
Imports:
matsbyname,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(dplyr,ungroup)
importFrom(magrittr,"%>%")
importFrom(matsbyname,coltype)
importFrom(matsbyname,elementquotient_byname)
importFrom(matsbyname,rowtype)
importFrom(matsbyname,setcoltype)
importFrom(matsbyname,setrowtype)
importFrom(purrr,set_names)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# matsindf 0.2.10 (2018-11-08)

* Breaking change: Default values of `rowtypes` and `coltypes` arguments to
`expand_to_tidy` and `collapse_to_matrices` functions are no longer `NULL`.
Rather, they are "`rowtypes`" and "`coltypes`".


# matsindf 0.2.9 (2018-11-06)

* Extended the `matsindf_apply` primer to include sections on using `matsindf_apply` with a data frame and programming.
Expand Down
25 changes: 12 additions & 13 deletions R/collapse.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#' such as "Commodities", "Industries", "Products", or "Machines".
#' The row and column types for the \pkg{matsindf}-style matrices are stored as attributes on the matrix
#' (\code{rowtype} and \code{coltype}),
#' which can be accessed with the \code{\link{rowtype}} and \code{\link{coltype}} functions
#' which can be accessed with the \code{\link[matsbyname]{rowtype}} and \code{\link[matsbyname]{coltype}} functions
#' of the \pkg{matsbyname} package.
#' Row and column types are both respected and propagated by the various \code{_byname} functions
#' of the \pkg{matsbyname} package.
#' Use the \code{_byname} functions when you do operations on the \pkg{matsindf}-style matrices.
#' Use the \code{*_byname} functions when you do operations on the \pkg{matsindf}-style matrices.
#' The \pkg{matsindf}-style matrices will be stored
#' in a column with same name as the incoming \code{values} column.

Expand Down Expand Up @@ -47,9 +47,9 @@
#' @param colnames a string identifying the column in \code{.DF} containing column names for matrices to be created.
#' Default is "\code{colnames}".
#' @param rowtypes optional string identifying the column in \code{.DF} containing the type of values in rows of the matrices to be created.
#' Default is \code{NULL}.
#' Default is "\code{rowtypes}".
#' @param coltypes optional string identifying the column in \code{.DF} containing the type of values in columns of the matrices to be created
#' Default is \code{NULL}.
#' Default is "\code{coltypes}".
#'
#' @return a data frame with matrices in columns
#'
Expand Down Expand Up @@ -93,19 +93,19 @@
#' "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),
#' rowtype = c( ptype, ptype, ptype, ptype, ptype, itype, itype,
#' ptype, ptype, ptype, ptype, NA, NA),
#' coltype = c( itype, itype, itype, itype, itype, ptype, ptype,
#' itype, itype, itype, itype, 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 = "rowtype", coltypes = "coltype")
#' rowtypes = "rowtypes", coltypes = "coltypes")
#' mats %>% spread(key = matrix, value = vals)
collapse_to_matrices <- function(.DF, matnames = "matnames", matvals = "matvals", rownames = "rownames", colnames = "colnames",
rowtypes = NULL, coltypes = NULL){
rowtypes = "rowtypes", coltypes = "coltypes"){
# Ensure that none of rownames, colnames, or values is a group variable.
# These can't be in the group variables.
# If they were, we wouldn't be able to summarise them into the matrices.
Expand All @@ -126,8 +126,7 @@ collapse_to_matrices <- function(.DF, matnames = "matnames", matvals = "matvals"
stop(paste("One of rowtypes or coltypes was non-NULL while the other was NULL.",
"Both need to be NULL or both need to be non-NULL in collapse_to_matrices."))
}
# If we get here, both rowtypes and coltypes have been changed from default (NULL) or
# both rowtypes and coltypes have not been changed from default (NULL).
# If we get here, both rowtypes and coltypes are not (NULL).
# Thus, we need to test only for the one of them being non-NULL.
.DF %>%
{if (!is.null(rowtypes)) {
Expand All @@ -138,7 +137,7 @@ collapse_to_matrices <- function(.DF, matnames = "matnames", matvals = "matvals"
dplyr::do(
# Convert .DF to matrices
!!matvals := rowcolval_to_mat(.data, rownames = rownames, colnames = colnames, matvals = matvals,
rowtype = rowtypes, coltype = coltypes)
rowtypes = rowtypes, coltypes = coltypes)
) %>%
select(!!!group_vars(.DF), !!matvals) %>%
data.frame(check.names = FALSE)
Expand Down
47 changes: 24 additions & 23 deletions R/expand.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
#' @param colnames name for the output column of column names (a string).
#' Default is "\code{colnames}".
#' @param rowtypes optional name for the output column of row types (a string).
#' Default is \code{NULL}.
#' Default is "\code{rowtypes}".
#' @param coltypes optional name for the output column of column types (a string)
#' Default is \code{NULL}.
#' Default is "\code{coltypes}".
#' @param drop if specified, the value to be dropped from output,
#' For example, \code{drop = 0} will cause \code{0} entries in the matrices to be deleted from output.
#' If \code{NA}, no values are dropped from output.
#' Default is \code{NA}.
#'
#' @return a tidy data frame containing expanded \pkg{matsindf}-style matrices
#'
#' @export
#'
#' @examples
Expand All @@ -41,27 +42,27 @@
#' 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),
#' rowtype = c( ptype, ptype, ptype, ptype, ptype, itype, itype,
#' ptype, ptype, ptype, ptype, NA, NA),
#' coltype = 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)
#' 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 = "rowtype", coltypes = "coltype",
#' rowtypes = "rowtypes", coltypes = "coltypes",
#' matvals = "vals") %>%
#' ungroup
#' ungroup()
#' expand_to_tidy(mats, matnames = "matrix", matvals = "vals",
#' rownames = "rows", colnames = "cols",
#' rowtypes = "rt", coltypes = "ct")
Expand All @@ -70,7 +71,7 @@
#' rowtypes = "rt", coltypes = "ct", drop = 0)
expand_to_tidy <- function(.DF, matnames = "matnames", matvals = "matvals",
rownames = "rownames", colnames = "colnames",
rowtypes = NULL, coltypes = NULL,
rowtypes = "rowtypes", coltypes = "coltypes",
drop = NA){
if (!is.data.frame(.DF) & is.list(.DF)) {
# Create an empty 1-row data frame with row names taken from .DF and promote to a column
Expand All @@ -87,7 +88,7 @@ expand_to_tidy <- function(.DF, matnames = "matnames", matvals = "matvals",
dplyr::do(
# Convert .data to row, col, val format
mat_to_rowcolval(.data[[matvals]][[1L]], rownames = rownames, colnames = colnames,
rowtype = rowtypes, coltype = coltypes,
rowtypes = rowtypes, coltypes = coltypes,
matvals = matvals, drop = drop)
) %>%
# Remove the grouping
Expand Down
Loading

0 comments on commit c836370

Please sign in to comment.