Keiku
2/10/2017 - 1:48 AM

Chi-square testing in each group.

Chi-square testing in each group.

library(dplyr)
library(purrr)
library(broom)

df <- data_frame(
  group = rep(letters[1:2], each = 50),
  cat1 = letters[round(runif(100) * 5) + 1],
  cat2 = letters[round(runif(100) * 3) + 1]
) 

df %>%
  split(.$group) %>%
  map(~chisq.test(.$cat1, .$cat2, simulate.p.value = TRUE, B = 10)) %>%
  map_df(glance)
# statistic   p.value parameter
# 1  14.69461 0.6363636        NA
# 2  15.23163 0.6363636        NA
# method
# 1 Pearson's Chi-squared test with simulated p-value\n\t (based on 10 replicates)
# 2 Pearson's Chi-squared test with simulated p-value\n\t (based on 10 replicates)