---
title: "Getting Started"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{getting-started}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(countmaskr)
library(knitr)
```

&nbsp;

# Code logic plot

![](logic_plot.png)

&nbsp;

# One dimensional frequency table



```{r 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()
```

&nbsp;


## Algorithm 1

```{r message=FALSE,error=FALSE,warning=FALSE}
aggregate_table %>%
  group_by(block) %>%
  mutate(N_masked = mask_counts(N)) %>%
  kable()
```

&nbsp;


## Algorithm 2

```{r message=FALSE,error=FALSE,warning=FALSE}
aggregate_table %>%
  group_by(block) %>%
  mutate(N_masked = mask_counts_2(N)) %>%
  kable()
```

&nbsp;

## Algorithm 3

```{r message=FALSE,error=FALSE,warning=FALSE}
aggregate_table %>%
  group_by(block) %>%
  mutate(N_masked = perturb_counts(N)) %>%
  kable()
```

&nbsp;


# Using `mask_table()`

mask_table() is a multi-tasking function which allows for masking, obtaining original and masked percentages on an aggregated table.

&nbsp;

## One-way masking on the original column.

```{r message=FALSE,error=FALSE,warning=FALSE}
mask_table(aggregate_table, group_by = "block", col_groups = list("N")) %>%
  kable()
```

&nbsp;

## One-way masking while preserving original column and creating new masked columns

Naming convention for the masked columns follow {col}_N_masked pattern.

```{r message=FALSE,error=FALSE,warning=FALSE}
mask_table(
  aggregate_table,
  group_by = "block",
  col_groups = list("N"),
  overwrite_columns = FALSE
) %>%
  kable()
```


&nbsp;

## Owo-way  masking with computing original and masked percentages

Naming convention for the original and masked percentages follow {col}_perc and {col}_perc_masked pattern.

```{r message=FALSE,error=FALSE,warning=FALSE}
mask_table(
  aggregate_table,
  group_by = "block",
  col_groups = list("N"),
  overwrite_columns = TRUE,
  percentages = TRUE
) %>%
  kable()
```


&nbsp;

# Two-way frequency table

```{r 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()
```