| table_apa | R Documentation |
table_apa() builds a publication-ready table by crossing one grouping
variable (group_var) with one or many row variables (row_vars), using
spicy::cross_tab() internally.
table_apa(
data,
row_vars,
group_var,
labels = NULL,
levels_keep = NULL,
include_total = TRUE,
drop_na = TRUE,
weights = NULL,
rescale = FALSE,
correct = FALSE,
simulate_p = FALSE,
simulate_B = 2000,
percent_digits = 1,
p_digits = 3,
v_digits = 2,
decimal_mark = ".",
output = c("wide", "long", "tinytable", "flextable", "excel", "clipboard", "word"),
style = c("auto", "raw", "report"),
indent_text = " ",
indent_text_excel_clipboard = " ",
add_multilevel_header = TRUE,
blank_na_wide = FALSE,
excel_path = NULL,
excel_sheet = "APA",
clipboard_delim = "\t",
word_path = NULL
)
data |
A data frame. |
row_vars |
Character vector of variable names to place in rows. |
group_var |
Single character variable name used for columns/groups. |
labels |
Optional character labels for |
levels_keep |
Optional character vector of levels to keep/order for row
modalities. If |
include_total |
Logical. If |
drop_na |
Logical. If |
weights |
Optional weights. Either |
rescale |
Logical. If |
correct |
Logical. If |
simulate_p |
Logical. If |
simulate_B |
Integer. Number of Monte Carlo replicates when
|
percent_digits |
Number of digits for percentages in report outputs.
Defaults to |
p_digits |
Number of digits for p-values (except |
v_digits |
Number of digits for Cramer's V. Defaults to |
decimal_mark |
Decimal separator ( |
output |
Output format: |
style |
|
indent_text |
Prefix used for modality labels in report table building.
Defaults to |
indent_text_excel_clipboard |
Stronger indentation used in Excel and clipboard exports. Defaults to six non-breaking spaces. |
add_multilevel_header |
Logical. If |
blank_na_wide |
Logical. If |
excel_path |
Path for |
excel_sheet |
Sheet name for Excel export. Defaults to |
clipboard_delim |
Delimiter for clipboard text export. Defaults to |
word_path |
Path for |
It supports raw data outputs (wide, long) and report-oriented outputs
(tinytable, flextable, excel, clipboard, word) with multi-level
headers, p-values, and Cramer's V.
Optional output engines require suggested packages:
tinytable for output = "tinytable"
flextable + officer for output = "flextable"/"word"
openxlsx for output = "excel"
clipr for output = "clipboard"
Depends on output and style:
"long" + "raw": long numeric data frame.
"wide" + "raw": wide numeric data frame.
"long" + "report": long formatted character data frame.
"wide" + "report": wide formatted character data frame.
"tinytable": a tinytable object.
"flextable": a flextable object.
"excel" / "clipboard" / "word": invisibly returns written object/path.
# Build a minimal reproducible dataset
d_ex <- transform(
mtcars,
hes = factor(gear, labels = c("BFH", "HEdS-Geneve", "HESAV")),
emploi_sf = ifelse(vs == 1, "Oui", "Non"),
role_prof_recherche = ifelse(am == 1, "Oui", "Non"),
w = mpg
)
# Raw long output (machine-friendly)
table_apa(
data = d_ex,
row_vars = c("emploi_sf", "role_prof_recherche"),
group_var = "hes",
labels = c("Emploi SF", "Role recherche"),
output = "long",
style = "raw"
)
# Raw wide output
table_apa(
data = d_ex,
row_vars = c("emploi_sf", "role_prof_recherche"),
group_var = "hes",
labels = c("Emploi SF", "Role recherche"),
output = "wide",
style = "raw"
)
# Weighted example
table_apa(
data = d_ex,
row_vars = c("emploi_sf", "role_prof_recherche"),
group_var = "hes",
labels = c("Emploi SF", "Role recherche"),
weights = "w",
rescale = TRUE,
simulate_p = FALSE,
output = "long",
style = "raw"
)
# Optional output: tinytable
if (requireNamespace("tinytable", quietly = TRUE)) {
tt_ex <- table_apa(
data = d_ex,
row_vars = c("emploi_sf", "role_prof_recherche"),
group_var = "hes",
labels = c("Emploi SF", "Role recherche"),
output = "tinytable"
)
}
# Optional output: Excel
if (requireNamespace("openxlsx", quietly = TRUE)) {
table_apa(
data = d_ex,
row_vars = c("emploi_sf", "role_prof_recherche"),
group_var = "hes",
labels = c("Emploi SF", "Role recherche"),
output = "excel",
excel_path = tempfile(fileext = ".xlsx")
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.