Takes a data frame or tabyl and formats it to display counts (N) and percentages combined into a single string cell (e.g., "12 (34.3%)").

adorn_np(x, denom = c("row", "col", "all"), digits = 1, trim = TRUE)

Arguments

x

A data frame or tabyl containing frequency data.

denom

A character string specifying the direction for percentage calculation. Must be one of "row" (default), "col", or "all".

digits

An integer specifying the number of decimal places to display for percentages. Default is 1.

trim

A logical scalar. If TRUE (default), trims and collapses whitespace in the output strings using str_squish. If FALSE, preserves padding created by formatting.

Value

A data frame (or tabyl) of character columns where each entry combines the raw count and percentage in the format "N (X.X%)".

Details

This function chains together several janitor package helpers:

  1. adorn_percentages to calculate relative proportions.

  2. adorn_pct_formatting to convert proportions to formatted percentage strings rounded "half up".

  3. adorn_ns to attach the raw counts (N) to the front of each formatted percentage string.

Examples

if (FALSE) { # \dontrun{
library(janitor)

# Create a basic tabyl
tab <- tabyl(mtcars, cyl, gear)

# Format with row percentages (default)
adorn_np(tab, denom = "row")

# Format with column percentages and 2 decimal places
adorn_np(tab, denom = "col", digits = 2)
} # }