Bird
Raised Fist0
R Programmingprogramming~20 mins

Chi-squared test in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Chi-squared Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a basic Chi-squared test in R
What is the output of this R code performing a Chi-squared test on a 2x2 table?
R Programming
data <- matrix(c(10, 20, 20, 40), nrow = 2)
result <- chisq.test(data)
result$p.value
A1.0000000
B0.9876543
C0.0012345
D0.0455003
Attempts:
2 left
💡 Hint
Check the p-value returned by chisq.test for the given matrix.
🧠 Conceptual
intermediate
1:30remaining
Understanding Chi-squared test assumptions
Which of the following is NOT an assumption of the Chi-squared test in R?
AObservations must be independent
BData must be normally distributed
CData should be categorical
DExpected frequencies should be at least 5 in each cell
Attempts:
2 left
💡 Hint
Think about the type of data Chi-squared test is designed for.
🔧 Debug
advanced
2:00remaining
Identify the error in Chi-squared test code
What error will this R code produce?
R Programming
data <- matrix(c(5, 3, 2), nrow = 3)
chisq.test(data)
AError: 'data' must have at least 2 columns and 2 rows
BError: 'data' must be a square matrix
CError: 'x' must be a matrix or a table
DNo error, test runs successfully
Attempts:
2 left
💡 Hint
Check the shape of the matrix and requirements for chisq.test.
🚀 Application
advanced
3:00remaining
Calculate Chi-squared statistic manually
Given the observed counts matrix in R: obs <- matrix(c(30, 10, 20, 40), nrow=2) Which code correctly calculates the Chi-squared test statistic manually (without using chisq.test)?
A
expected &lt;- outer(rowSums(obs), colSums(obs)) / sum(obs)
chisq &lt;- sum((obs - expected)^2 / expected)
chisq
B
expected &lt;- rowSums(obs) * colSums(obs) / sum(obs)
chisq &lt;- sum((obs - expected)^2 / expected)
chisq
C
expected &lt;- outer(rowSums(obs), colSums(obs)) * sum(obs)
chisq &lt;- sum((obs - expected)^2 / expected)
chisq
D
expected &lt;- outer(rowSums(obs), colSums(obs)) / sum(obs)
chisq &lt;- sum((expected - obs)^2 / obs)
chisq
Attempts:
2 left
💡 Hint
Expected counts are calculated by multiplying row and column sums divided by total sum.
❓ Predict Output
expert
2:30remaining
Output of Chi-squared test with simulated data
What is the output of this R code snippet?
R Programming
set.seed(123)
data <- matrix(sample(1:100, 9), nrow=3)
result <- chisq.test(data)
round(result$statistic, 2)
A4.67
B7.89
C5.12
D2.34
Attempts:
2 left
💡 Hint
Run the code with the given seed to get the Chi-squared statistic.

Practice

(1/5)
1. What does the Chi-squared test in R primarily check for?
easy
A. Whether two categorical variables are related
B. Whether two numerical variables have the same mean
C. Whether a variable follows a normal distribution
D. Whether the data contains missing values

Solution

  1. Step 1: Understand the purpose of the Chi-squared test

    The Chi-squared test is used to check if there is an association between two categorical variables.
  2. Step 2: Compare with other options

    Options A and B relate to numerical data tests, and D is about data quality, not relationships.
  3. Final Answer:

    Whether two categorical variables are related -> Option A
  4. Quick Check:

    Chi-squared test checks categorical variable relationship = C [OK]
Hint: Chi-squared tests relationships between categories, not numbers [OK]
Common Mistakes:
  • Confusing Chi-squared with t-test or ANOVA
  • Thinking it tests numerical data means
  • Assuming it checks data normality
2. Which of the following is the correct way to perform a Chi-squared test on a table named tbl in R?
easy
A. chisq(tbl)
B. chisq.test(tbl)
C. chi.test(tbl)
D. test.chisq(tbl)

Solution

  1. Step 1: Recall the correct function name in R

    The function to perform a Chi-squared test in R is chisq.test().
  2. Step 2: Check the syntax

    The correct syntax is calling chisq.test() with the table as the argument.
  3. Final Answer:

    chisq.test(tbl) -> Option B
  4. Quick Check:

    Correct function is chisq.test() = B [OK]
Hint: Use chisq.test() function for Chi-squared test in R [OK]
Common Mistakes:
  • Using incorrect function names like chisq() or chi.test()
  • Placing the function name after the table
  • Forgetting parentheses after function name
3. What will be the output of the following R code?
tbl <- matrix(c(10, 20, 30, 40), nrow=2)
result <- chisq.test(tbl)
round(result$p.value, 3)
medium
A. 0.504
B. 0.025
C. 0.000
D. Error: object 'result' not found

Solution

  1. Step 1: Understand the input table

    The matrix has counts: 10, 20 in first row and 30, 40 in second row.
  2. Step 2: Perform Chi-squared test and check p-value

    Running chisq.test on this table gives a p-value around 0.504, meaning no strong association.
  3. Final Answer:

    0.504 -> Option A
  4. Quick Check:

    Chi-squared p-value rounded = 0.504 [OK]
Hint: Check p-value from chisq.test() to judge significance [OK]
Common Mistakes:
  • Confusing p-value with test statistic
  • Expecting zero p-value for small counts
  • Mistaking error message for output
4. Identify the error in this R code snippet for Chi-squared test:
data <- matrix(c(5, 15, 20, 10), ncol=2)
chisq.test(data)
medium
A. Matrix should have rows and columns swapped
B. chisq.test() requires a data frame, not a matrix
C. No error, code runs correctly
D. Matrix must be named 'tbl' to work

Solution

  1. Step 1: Check matrix creation

    The matrix is created with 4 values and 2 columns, which is valid.
  2. Step 2: Check chisq.test() input type

    chisq.test() accepts a matrix of counts, so this input is valid and runs without error.
  3. Final Answer:

    No error, code runs correctly -> Option C
  4. Quick Check:

    chisq.test() accepts matrix input = A [OK]
Hint: chisq.test() works with matrices or tables of counts [OK]
Common Mistakes:
  • Thinking chisq.test() only accepts data frames
  • Assuming matrix dimensions must be square
  • Believing variable name affects function
5. You have survey data of 3 groups choosing among 3 products. How do you apply a Chi-squared test in R to check if product preference depends on group?
group <- c('A', 'A', 'B', 'B', 'C', 'C')
product <- c('X', 'Y', 'X', 'Z', 'Y', 'Z')
# What next?
hard
A. Convert group and product to numeric and run chisq.test()
B. Run chisq.test(group, product) directly without table
C. Use t.test() on group and product vectors
D. Create a table with table(group, product) then run chisq.test() on it

Solution

  1. Step 1: Create a contingency table

    Use table(group, product) to get counts of each group-product combination.
  2. Step 2: Apply Chi-squared test on the table

    Run chisq.test() on the table to test independence between group and product preference.
  3. Final Answer:

    Create a table with table(group, product) then run chisq.test() on it -> Option D
  4. Quick Check:

    Table then chisq.test() = A [OK]
Hint: Always make a table before chisq.test() for categorical data [OK]
Common Mistakes:
  • Trying to run chisq.test() on raw vectors
  • Using t.test() instead of chisq.test() for categories
  • Converting categories to numbers incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard