Ann Ng
Ann Ng

Reputation: 45

apply chi-square test in R for each level of a categorical variable

I'm having a data frame as below. I want to run a chi-square test between 'placement' and 'books_quantile' variables at each level of zip codes. I've tried a few ways but not successful yet. Can somebody help?

Thank you!

str(zip2)
tibble [10,748 x 3] (S3: tbl_df/tbl/data.frame)
 $ placement     : Factor w/ 5 levels "3 or More Grade Levels Below",..: 5 3 3 3 5 2 2 5 3 5 ...
 $ books_quantile: Factor w/ 4 levels "Q1 (>=56 books)",..: 2 2 2 2 3 3 3 2 1 2 ...
 $ zip           : Factor w/ 24 levels "38016","38018",..: 11 21 9 8 22 12 15 15 13 12 ...

Upvotes: 0

Views: 858

Answers (1)

Onyambu
Onyambu

Reputation: 79348

You should do:

apply(xtabs(~placement + books_quantile + zip, zip2), 3, chisq.test)

Upvotes: 1

Related Questions