APA Chi-Square Crosstabs Table

Description

Creates an APA-formatted crosstabulation table showing observed counts, row percentages, row and column totals, and chi-square test results in the footer. Can produce either a filled answer KEY or a blank template.

Usage

create_chi_crosstabs_table(
  chi_results_list = NULL,
  var1_name = "Variable 1",
  var2_name = "Variable 2",
  var1_labels = c("Level 1", "Level 2"),
  var2_labels = c("Group 1", "Group 2"),
  KEY = TRUE,
  include_percentages = TRUE,
  table_title = NULL
)

Arguments

chi_results_list Output from chi_square_answers() or NULL (for blank table when KEY = FALSE).
var1_name Character. Display name for the row variable.
var2_name Character. Display name for the column variable (appears as a spanning header).
var1_labels Character vector of length 2. Display labels for row variable levels.
var2_labels Character vector of length 2. Display labels for column variable levels.
KEY Logical. If TRUE (default), fill with computed values. If FALSE, create a blank template.
include_percentages Logical. If TRUE (default), include row percentages alongside counts (e.g., "15 (62.5%)").
table_title Character or NULL. Optional table caption.

Value

A flextable::flextable() object.

Examples

library("psych350lab")

data(superman, package = "psych350data")
result <- chi_square_answers(superman, "clark_grp", "tomatometer")

# Filled table with percentages
ft <- create_chi_crosstabs_table(
  chi_results_list = result,
  var1_name = "Height Group",
  var2_name = "Tomatometer",
  var1_labels = c("Under 6ft", "6ft+"),
  var2_labels = c("Rotten", "Fresh"),
  KEY = TRUE,
  include_percentages = TRUE,
  table_title = "Table 1. Crosstabulation of Height Group and Tomatometer Status"
)
ft

# Filled table without percentages
ft_counts <- create_chi_crosstabs_table(
  chi_results_list = result,
  var1_name = "Height Group",
  var2_name = "Tomatometer",
  var1_labels = c("Under 6ft", "6ft+"),
  var2_labels = c("Rotten", "Fresh"),
  KEY = TRUE,
  include_percentages = FALSE
)
ft_counts

# Blank table
ft_blank <- create_chi_crosstabs_table(
  var1_name = "Height Group",
  var2_name = "Tomatometer",
  var1_labels = c("Under 6ft", "6ft+"),
  var2_labels = c("Rotten", "Fresh"),
  KEY = FALSE
)
ft_blank