## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(countmaskr)
library(knitr)

## ----message=FALSE,error=FALSE,warning=FALSE----------------------------------
data("countmaskr_data")

aggregate_table <- countmaskr_data %>%
  select(-c(id, age)) %>%
  gather(block, Characteristics) %>%
  group_by(block, Characteristics) %>%
  summarise(N = n()) %>%
  ungroup()

## ----message=FALSE,error=FALSE,warning=FALSE----------------------------------
aggregate_table %>%
  group_by(block) %>%
  mutate(N_masked = mask_counts(N)) %>%
  kable()

## ----message=FALSE,error=FALSE,warning=FALSE----------------------------------
aggregate_table %>%
  group_by(block) %>%
  mutate(N_masked = mask_counts_2(N)) %>%
  kable()

## ----message=FALSE,error=FALSE,warning=FALSE----------------------------------
aggregate_table %>%
  group_by(block) %>%
  mutate(N_masked = perturb_counts(N)) %>%
  kable()

## ----message=FALSE,error=FALSE,warning=FALSE----------------------------------
mask_table(aggregate_table, group_by = "block", col_groups = list("N")) %>%
  kable()

## ----message=FALSE,error=FALSE,warning=FALSE----------------------------------
mask_table(
  aggregate_table,
  group_by = "block",
  col_groups = list("N"),
  overwrite_columns = FALSE
) %>%
  kable()

## ----message=FALSE,error=FALSE,warning=FALSE----------------------------------
mask_table(
  aggregate_table,
  group_by = "block",
  col_groups = list("N"),
  overwrite_columns = TRUE,
  percentages = TRUE
) %>%
  kable()

## ----message=FALSE,error=FALSE,warning=FALSE----------------------------------
two_way_freq_table <- countmaskr_data %>%
  count(race, gender) %>%
  pivot_wider(names_from = gender, values_from = n) %>%
  mutate(
    across(all_of(c("Female", "Male", "Other")), ~ ifelse(is.na(.), 0, .)),
    Overall = Female + Male + Other,
    .after = 1
  )

mask_table(
  two_way_freq_table,
  col_groups = list(c("Overall", "Female", "Male", "Other")),
  overwrite_columns = TRUE,
  percentages = FALSE
) %>%
  kable()

