apa_format_columns: Format data columns

View source: R/formatting.R

apa_format_columnsR Documentation

Format data columns

Description

apa_format_columns() formats the columns of a data frame of results and renames their headers in APA style, using a set of column_formats(). apa_flextable() calls it when auto_format_columns = TRUE.

Usage

apa_format_columns(
  data,
  column_formats = NULL,
  no_format_columns = NULL,
  rename_headers = TRUE,
  latex_headers = FALSE,
  format_separated_headers = TRUE,
  sep = "_",
  accuracy = NULL
)

Arguments

data

data set (data.frame or tibble)

column_formats

column_formats object. If NULL, the current default formatter set with apa7_defaults() will be used.

no_format_columns

Column name or tidyselect function. selected columns are not formatted

rename_headers

if TRUE, rename headers with markdown or latex

latex_headers

if TRUE, rename headers with latex instead of markdown

format_separated_headers

if TRUE, format headers with separated names. For example, if the formatter formats column R2 as ⁠*R*^2^⁠, then ⁠Model 1_R2⁠ becomes ⁠Model 1_*R*^2^⁠)

sep

separator for separated headers (default is "_")

accuracy

numeric (default: NULL, uses the current default accuracy set with apa7_defaults()). If not NULL, sets the accuracy for the formatter.

Details

Each column is matched by name to a column_format() in column_formats (e.g., p, SE, beta, or R2). The values of a matched column are formatted by the format's formatter, and its name is replaced by the format's markdown header (e.g., beta becomes ⁠*β*⁠), or by its LaTeX header if latex_headers = TRUE. Columns without a format, and columns named in no_format_columns, are left as they are.

If format_separated_headers = TRUE (default), a column name with a separator (sep, an underscore by default) is matched by its last part, so ⁠Model 1_R2⁠ is formatted like R2 and becomes ⁠Model 1_*R*^2^⁠. In apa_flextable(), the part before the separator becomes a column spanner. Format names that contain an underscore themselves (e.g., R2_adjusted) are matched whole.

A few columns are matched by a pattern or combined:

  • CI, CI_low, and CI_high (e.g., from parameters::parameters()) become a single column of intervals, with a header such as "95% CI" that gives the confidence level stored in CI. Prefixed columns are combined the same way for each column spanner (e.g., ⁠Model 1_CI⁠, ⁠Model 1_CI_low⁠, and ⁠Model 1_CI_high⁠ become ⁠Model 1_95% CI⁠). A CI column without its bounds is left as it is.

  • If there are t and df_error columns, the degrees of freedom are moved into the t header (e.g., t(28)), and df_error is dropped.

  • A column named like t(28) is formatted with the t_df format (and gets the header t(28)), and one named like ⁠90% CI⁠ with the CI_percent format.

To see the default formats, run column_formats()@get_tibble. The article Building Tables by Hand shows how to change them.

Value

tibble

Examples

lm(mpg ~ cyl + wt, data = mtcars) |>
  parameters::parameters() |>
  apa_format_columns() |>
  apa_flextable()

apa7 documentation built on Dec. 1, 2026, 5:06 p.m.