diff --git a/R/acro_tables.R b/R/acro_tables.R index 0e4d1b8..6093ed4 100644 --- a/R/acro_tables.R +++ b/R/acro_tables.R @@ -3,17 +3,26 @@ #' @param index Values to group by in the rows. #' @param columns Values to group by in the columns. #' @param values Array of values to aggregate according to the factors. Requires `aggfunc` be specified. +#' @param rownames If passed, must match number of row arrays passed. +#' @param colnames If passed, must match number of column arrays passed. #' @param aggfunc If specified, requires `values` be specified as well. +#' @param margins dd row/column margins (subtotals). +#' @param margins_name Name of the row/column that will contain the totals when margins is True. +#' @param dropna Do not include columns whose entries are all NaN. +#' @param normalize Normalize by dividing all values by the sum of values. +#' @param show_suppressed how the totals are being calculated when the suppression is true #' #' @return Cross tabulation of the data #' @export -acro_crosstab <- function(index, columns, values = NULL, aggfunc = NULL) { +acro_crosstab <- function(index, columns, values = NULL, rownames = NULL, colnames = NULL, aggfunc = NULL, margins = FALSE, margins_name = "All", dropna = TRUE, normalize = FALSE, show_suppressed = FALSE) { if (is.null(acroEnv$ac)) { stop("ACRO has not been initialised. Please first call acro_init()") } - py_table <- acroEnv$ac$crosstab(index, columns, values = values, aggfunc = aggfunc) + + py_table <- acroEnv$ac$crosstab(index, columns, values = values, rownames = rownames, colnames = colnames, aggfunc = aggfunc, margins = margins, margins_name = margins_name, dropna = dropna, normalize = normalize, show_suppressed = show_suppressed) table <- reticulate::py_to_r(py_table) + return(table) } diff --git a/man/acro_crosstab.Rd b/man/acro_crosstab.Rd index 168cfad..50c8192 100644 --- a/man/acro_crosstab.Rd +++ b/man/acro_crosstab.Rd @@ -4,7 +4,19 @@ \alias{acro_crosstab} \title{Compute a simple cross tabulation of two (or more) factors.} \usage{ -acro_crosstab(index, columns, values = NULL, aggfunc = NULL) +acro_crosstab( + index, + columns, + values = NULL, + rownames = NULL, + colnames = NULL, + aggfunc = NULL, + margins = FALSE, + margins_name = "All", + dropna = TRUE, + normalize = FALSE, + show_suppressed = FALSE +) } \arguments{ \item{index}{Values to group by in the rows.} @@ -13,7 +25,21 @@ acro_crosstab(index, columns, values = NULL, aggfunc = NULL) \item{values}{Array of values to aggregate according to the factors. Requires \code{aggfunc} be specified.} +\item{rownames}{If passed, must match number of row arrays passed.} + +\item{colnames}{If passed, must match number of column arrays passed.} + \item{aggfunc}{If specified, requires \code{values} be specified as well.} + +\item{margins}{dd row/column margins (subtotals).} + +\item{margins_name}{Name of the row/column that will contain the totals when margins is True.} + +\item{dropna}{Do not include columns whose entries are all NaN.} + +\item{normalize}{Normalize by dividing all values by the sum of values.} + +\item{show_suppressed}{how the totals are being calculated when the suppression is true} } \value{ Cross tabulation of the data diff --git a/tests/testthat/test-acro_crosstab.R b/tests/testthat/test-acro_crosstab.R index e648301..30e5002 100644 --- a/tests/testthat/test-acro_crosstab.R +++ b/tests/testthat/test-acro_crosstab.R @@ -18,3 +18,13 @@ test_that("acro_crosstab works", { table <- acro_crosstab(index = nursery_data[, c("health")], columns = nursery_data[, c("finance")]) expect_equal(table[, -1, drop = FALSE], expected_table[, -1, drop = FALSE]) }) + +test_that("acro_crosstab works with margins", { + acro_init() + p_table <- acro_crosstab(index = nursery_data[, c("health")], columns = nursery_data[, c("finance")], margins = TRUE) + + # Create an R table with margins + r_table <- addmargins(table(nursery_data[, c("health")], columns = nursery_data[, c("finance")])) + + expect_equal(unname(as.matrix(r_table)), unname(as.matrix(p_table))) +})