Hi @sai_matcha,
Here are a couple of options using add-on packages:
library(chisq.posthoc.test)
# Example
M <- as.table(rbind(c(762, 327, 468), c(484, 239, 477)))
dimnames(M) <- list(gender = c("F", "M"),
party = c("Democrat","Independent", "Republican"))
M
#> party
#> gender Democrat Independent Republican
#> F 762 327 468
#> M 484 239 477
# Pass data matrix to chisq.posthoc.test function
chisq.posthoc.test(M)
#> Dimension Value Democrat Independent Republican
#> 1 F Residuals 4.502054 0.6994517 -5.315946
#> 2 F p values 0.000040 1.0000000 0.000001
#> 3 M Residuals -4.502054 -0.6994517 5.315946
#> 4 M p values 0.000040 1.0000000 0.000001
chisq.posthoc.test(M,method = "bonferroni")
#> Dimension Value Democrat Independent Republican
#> 1 F Residuals 4.502054 0.6994517 -5.315946
#> 2 F p values 0.000040 1.0000000 0.000001
#> 3 M Residuals -4.502054 -0.6994517 5.315946
#> 4 M p values 0.000040 1.0000000 0.000001
# Alternative package/function
library(RVAideMemoire)
#> *** Package RVAideMemoire v 0.9-81-2 ***
chisq.theo.multcomp(M, p.method = "bonferroni")
#>
#> Pairwise comparisons using chi-squared tests
#>
#> data: M and bonferroni
#>
#> observed.gender observed.party observed.Freq expected Chi Pr(>Chi)
#> F Democrat 762 459.5 238.9717 3.954e-53 ***
#> M Democrat 484 459.5 1.5676 1.000e+00
#> F Independent 327 459.5 45.8487 7.665e-11 ***
#> M Independent 239 459.5 126.9734 1.130e-28 ***
#> F Republican 468 459.5 0.1887 1.000e+00
#> M Republican 477 459.5 0.7998 1.000e+00
#>
#> P value adjustment method: bonferroni
Created on 2022-10-07 with reprex v2.0.2
Hope this helps.