I have a review frame containing several questions (columns) encoded as 1 = agree / 0 = disagree. Respondents (lines) are classified according to the metrics of "age" ("young", "middle", "old"), "region" ("East", "Middle", "West"), etc. There are about 30 categories in total (3 ages, 3 regions, 2 genders, 11 classes, etc.). Within each metric, categories do not overlap and have different sizes.
This models the cut version of the dataset:
n<-400
set.seed(1)
data<-data.frame(age=sample(c('young','middle','old'),n,replace=T),region=sample(c('East','Mid','West'),n,replace=T),gender=sample(c('M','F'),n,replace=T),Q15a=sample(c(0,1),n,replace=T),Q15b=sample(c(0,1),n,replace=T))
I can use Chi-square to check if the answers, say, in the West, are significantly different from the general sample, for Q15a, with:
attach(data)
chisq.test(table(subset(data,region=='West')$Q15a),p=table(Q15a),rescale.p=T)
I want to check all categories against the general sample for Q15a, and then for ~ 20 other questions. Since about 30 tests were asked for each question, I want to find a way (efficiently or otherwise) to automate this, but I'm struggling to figure out how to get R to do it myself or how to write a loop to cycle through the categories. I searched [1] and was distracted by pairwise comparison testing using parwise.prop.test (), but so far I have not found anything that really answers it.
[1] similar but non-duplicate questions (both tests in columns):
Using Loops to Run a Chi-Square Test in R
Square Analysis using a for loop in R