Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions R/acro_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
28 changes: 27 additions & 1 deletion man/acro_crosstab.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-acro_crosstab.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})
Loading