Track 10 · Comparing Groups and Categorical Data · Lesson 1 of 11
Contingency tables
Build a contingency table for two categorical variables and read its joint, marginal and conditional distributions correctly.
A marketing team asks whether customers who sign up through email go on to buy more often than those from display ads. A hospital board asks whether recovery differs between its two sites. Both questions are about two categorical variables measured on the same people, and both are answered — or misanswered — by the percentages you take from one table.
A contingency table (or cross-tabulation) counts how many units fall into each combination of two categorical variables. With categories for the row variable and for the column variable it has cells. The row and column totals in the margins are called the marginal totals, and the bottom-right corner holds the grand total .
Every unit must be counted in exactly one cell: the categories of each variable must be exhaustive and mutually exclusive, and each unit (patient, customer, respondent) appears once.
| Recovered | Not recovered | Total | |
|---|---|---|---|
| Hospital A | 395 | 105 | 500 |
| Hospital B | 430 | 70 | 500 |
| Total | 825 | 175 | 1000 |
From one table you can read three different kinds of distribution. They answer different questions, and confusing them is the most common error in reading cross-tabulations.
- the count in row , column
- the total of row (the dot means “summed over the columns”); is the total of column
- the grand total
- the proportion of row that falls in column — a row percentage. The column percentage conditions on the column instead
Worked example
Reading the hospital table three ways
Use the hospital table above.
Joint. 395 of 1000 patients were treated at Hospital A and recovered: .
Marginal. Overall 825 of 1000 recovered: . This ignores the hospital entirely.
Conditional on hospital (row %). Hospital A: recovered; Hospital B: . These are the numbers that compare the hospitals.
Conditional on outcome (column %). Of the 825 patients who recovered, were at Hospital A. This answers a different question (“where did recovered patients come from?”) and mostly reflects how many patients each hospital treats.
To compare groups, condition on the group: compute percentages within each hospital, so groups of different sizes become comparable.
A third variable can change the story
Split the same patients by severity on admission. Among severe cases Hospital A's recovery rate is 75.0% against 70.0% at B; among mild cases it is 95.0% against 90.0%. Hospital A does better within both severity groups, yet worse overall, because it treats most of the severe cases. This reversal is Simpson's paradox: a two-way table summarises the data honestly, but the comparison it suggests can be confounded by a variable it leaves out.
| Severity | Hospital A | Hospital B |
|---|---|---|
| Mild | 95.0% of 100 | 90.0% of 400 |
| Severe | 75.0% of 400 | 70.0% of 100 |
| All patients | 79.0% of 500 | 86.0% of 500 |
Tables from aggregated counts
Data often arrive already counted. Each row of campaign_funnel is one channel-week with counts of signups and of purchases by those signups. To build the table channel × purchased (yes/no) among signups, add the counts over all weeks: yes = purchases, no = signups − purchases. Email gives 30.7% purchasing (3,903 of 12,708 signups) and Display 12.9% (617 of 4,768).
Dataset
Marketing funnel by channel
130 rows · one row = one channel-week (aggregated counts) · synthetic data
| Week start | Channel | Clicks (clicks) | Signups (signups) | Purchases (orders) |
|---|---|---|---|---|
| 2024-01-07 | Search | 6282 | 714 | 158 |
| 2024-01-07 | Social | 5666 | 473 | 56 |
| 2024-01-07 | 2991 | 450 | 130 | |
| 2024-01-07 | Display | 2616 | 162 | 22 |
| 2024-01-07 | Affiliate | 1688 | 239 | 67 |
| 2024-01-14 | Search | 6962 | 909 | 203 |
One row per channel-week. Pool the counts over the 26 weeks before computing rates.
Quick check
A survey table has department in the rows and work mode (onsite, hybrid, remote) in the columns. Which percentages show whether work arrangements differ between departments?
Exercises
The lesson is complete when every exercise is answered correctly. Hints are free; viewing a worked solution before solving means the exercise won't count towards mastery.
0 of 7 exercises correct (not saved — sign in to keep your progress)
Exercise 1 · Guided, difficulty 1 of 3
Joint, marginal and conditional
A bank surveyed customers about the channel they use most:
| Age group | App | Website | Phone | Total |
|---|---|---|---|---|
| Under 35 | 31 | 25 | 81 | 137 |
| 35 and over | 18 | 66 | 42 | 126 |
| Total | 49 | 91 | 123 | 263 |
Give each proportion to 3 decimal places.
- The joint proportion of all customers who are under 35 and prefer the website.
- The marginal proportion of all customers who prefer the phone.
- The conditional proportion of under-35 customers who prefer the website.
- The proportion of website users who are under 35.
Exercise 2 · Interpretation, difficulty 1 of 3
Which percentage answers the question?
A retailer cross-tabulates customer type (new, returning, loyalty; rows) against payment method (card, cash, wallet, buy-now-pay-later; columns). The manager asks: “Do loyalty customers pay by card more often than new customers?” Which numbers should you compare?
Exercise 3 · Application, difficulty 1 of 3
Recovery rate at each hospital
In hospital_outcomes, cross-tabulate hospital against recovered. What percentage of each hospital's patients recovered? Give both to 1 decimal place.
Data: hospital_outcomes · Open in the lab to explore and calculate.
Exercise 4 · Application, difficulty 2 of 3
Recovery among severe cases
Now keep only the patients with severity = severe. What percentage recovered at each hospital? Give both to 1 decimal place.
Data: hospital_outcomes (severity = severe) · Open in the lab to explore and calculate.
Exercise 5 · Application, difficulty 2 of 3
Purchase rate by channel from weekly counts
In campaign_funnel, what percentage of signups made a purchase over the whole 26 weeks, for Email and for Display? Pool the counts over weeks. Give both to 2 decimal places.
Data: campaign_funnel · Open in the lab to explore and calculate.
Exercise 6 · Practice, difficulty 1 of 3
Joint, marginal or conditional?
A table cross-classifies employees by department (rows) and work mode (columns). Classify the calculation each question needs.
Exercise 7 · Interpretation, difficulty 2 of 3
Describe work arrangements by department
The engagement_survey table of department × work mode (respondents) is:
| Department | Onsite | Hybrid | Remote | Total |
|---|---|---|---|---|
| Operations | 136 | 44 | 9 | 189 |
| Customer Service | 124 | 35 | 6 | 165 |
| Sales | 32 | 77 | 34 | 143 |
| Technology | 47 | 66 | 38 | 151 |
| Finance | 24 | 39 | 19 | 82 |
| Human Resources | 19 | 19 | 10 | 48 |
| Total | 382 | 280 | 116 | 778 |
In 2–3 sentences, describe how work arrangements differ between departments. Use percentages that make departments of different sizes comparable, and be careful about what the table can and cannot tell you.
What you can now do
- Build a contingency table from raw categorical data or from aggregated counts
- Compute joint, marginal and conditional proportions and say which question each answers
- Compare groups with percentages conditioned on the group
- Recognise that a third variable can reverse a two-way comparison (Simpson's paradox)