Skip to content

Instantly share code, notes, and snippets.

@bschneidr
Created October 26, 2023 01:49
Show Gist options
  • Save bschneidr/f36ae41255a956be02bf1e4ec4e65dae to your computer and use it in GitHub Desktop.
Save bschneidr/f36ae41255a956be02bf1e4ec4e65dae to your computer and use it in GitHub Desktop.
library(dplyr)
library(broom)
library(tidyr)
# Make example data
my_data <- data.frame(x = sample(1:5, size = 10, replace = TRUE),
y = sample(1:5, size = 10, replace = TRUE))
# Generate every pair of variables
var_list <- colnames(my_data)
var_combinations <- expand.grid(Var_1 = var_list, Var_2 = var_list,
stringsAsFactors = FALSE)
var_combinations |>
# Group data by rows
rowwise() |>
# Do a Chi-square test and tidy the output
mutate(
chisq_test = chisq.test(x = my_data[[Var_1]],
y = my_data[[Var_2]]) |>
broom::tidy()
) |>
unnest(cols = chisq_test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment