Chi-square test of independence for two categorical columns in R

Quick Overview

Write R functions that build a contingency table from two categorical columns, run Pearson's chi-square test of independence, and return a decision at a chosen significance level. It tests table(), the chisq.test result object, the p-value decision rule and when the approximation breaks down.

Chi-square test of independence for two categorical columns in R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement three R functions that cross-tabulate two categorical columns of a data frame and test their independence with Pearson's chi-square test. - `build_contingency_table(df, row_var, col_var)` takes the column names as strings and returns a 2D `table()` of counts, with `df[[row_var]]` as rows and `df[[col_var]]` as columns. - `run_chisq_test(tbl, correct = FALSE)` runs `chisq.test(tbl, correct = correct)` and returns a list with `$statistic` (unnamed numeric scalar), `$df` (integer), `$p_value` (numeric) and `$expected` (numeric matrix with the same dimensions as `tbl`). - `is_independent(tbl, alpha = 0.05, correct = FALSE)` returns a logical scalar that is `TRUE` if and only if `p_value > alpha`. It uses `run_chisq_test` internally. Example: ```r df <- data.frame( sex = c("M","F","M","F","M","F"), pref = c("A","A","B","B","A","B") ) tbl <- build_contingency_table(df, "sex", "pref") res <- run_chisq_test(tbl, correct = FALSE) res$statistic # unnamed numeric X-squared res$df # integer degrees of freedom res$p_value # numeric p-value is_independent(tbl) # TRUE / FALSE ``` ```hint Read the test object first Run `str(chisq.test(tbl))` and check which fields carry names or a type that differs from what the contract asks for. ``` ```hint State the null hypothesis Write down what the null hypothesis of this test is, then decide which side of `alpha` means you fail to reject it. ``` ### Constraints & Assumptions - `row_var` and `col_var` are names of categorical (character or factor) columns in `df`. - `correct` is Yates' continuity correction, which `chisq.test` applies only to 2x2 tables. ### What a Strong Answer Covers - Building the table with the row variable as rows and the column variable as columns. - Pulling the statistic, degrees of freedom, p-value and expected counts out of the `htest` object with the exact names and types required. - The decision rule `p_value > alpha`, and what a `TRUE` result does and does not let you conclude. - When the chi-square approximation is unreliable (small expected counts, empty rows or columns, a variable with one level) and how R behaves in those cases. ### Follow-up Questions - When would you apply the continuity correction (`correct = TRUE`)? - How would you handle low expected counts that make the chi-square approximation unreliable? - If `is_independent` returns `TRUE`, can you conclude that the two variables are independent? - Once the test rejects independence, how would you measure the strength of the association?

Overview: Write R functions that build a contingency table from two categorical columns, run Pearson's chi-square test of independence, and return a decision at a chosen significance level. It tests table(), the chisq.test result object, the p-value decision rule and when the approximation breaks down.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement three R functions that cross-tabulate two categorical columns of a data frame and test their independence with Pearson's chi-square test.

  • build_contingency_table(df, row_var, col_var) takes the column names as strings and returns a 2D table() of counts, with df[[row_var]] as rows and df[[col_var]] as columns.
  • run_chisq_test(tbl, correct = FALSE) runs chisq.test(tbl, correct = correct) and returns a list with $statistic (unnamed numeric scalar), $df (integer), $p_value (numeric) and $expected (numeric matrix with the same dimensions as tbl ).
  • is_independent(tbl, alpha = 0.05, correct = FALSE) returns a logical scalar that is TRUE if and only if p_value > alpha . It uses run_chisq_test internally.

Example:

df <- data.frame(
  sex = c("M","F","M","F","M","F"),
  pref = c("A","A","B","B","A","B")
)
tbl <- build_contingency_table(df, "sex", "pref")
res <- run_chisq_test(tbl, correct = FALSE)
res$statistic   # unnamed numeric X-squared
res$df          # integer degrees of freedom
res$p_value     # numeric p-value
is_independent(tbl)  # TRUE / FALSE

Constraints & Assumptions

  • row_var and col_var are names of categorical (character or factor) columns in df .
  • correct is Yates' continuity correction, which chisq.test applies only to 2x2 tables.

What a Strong Answer Covers Guidance

  • Building the table with the row variable as rows and the column variable as columns.
  • Pulling the statistic, degrees of freedom, p-value and expected counts out of the htest object with the exact names and types required.
  • The decision rule p_value > alpha , and what a TRUE result does and does not let you conclude.
  • When the chi-square approximation is unreliable (small expected counts, empty rows or columns, a variable with one level) and how R behaves in those cases.

Follow-up Questions Guidance

  • When would you apply the continuity correction ( correct = TRUE )?
  • How would you handle low expected counts that make the chi-square approximation unreliable?
  • If is_independent returns TRUE , can you conclude that the two variables are independent?
  • Once the test rejects independence, how would you measure the strength of the association?