At ZPD, Deputy Chief Okonkwo drops by again: “Numbers are great, but I also need to know who’s where. Break the recruits down by group for me.”
In the last lecture, we described continuous variables: things measured on a number line, like a score or an income.
Today we will describe categorical variables: things that sort recruits into groups rather than measuring them on a scale.
R Set Up| resident_name | department | district | academy_status |
|---|---|---|---|
| Sable Jones | Desk | Rainforest District | Fail |
| Kevin Frostwhisker | Patrol | Downtown | Fail |
| Jasper Duskrunner | Patrol | Sahara Square | Pass |
| Greta Smith | Desk | Tundratown | Pass |
| Otto Meadowsly | Patrol | Downtown | Pass |
Categorical Data
For categorical variables, we describe data by counting how many observations fall into each category, not by averaging values.
We have two main tools for describing a categorical variable:
Frequency
The frequency of a category is simply a count: how many observations fall into that group.
Frequencies answer the question: how many?
Frequencies are useful, but interpreting them depends on the size of the dataset.
Relative Frequency
The relative frequency (proportion), is the frequency divided by the total number of observations.
\text{proportion} = \frac{\text{count in category}}{\text{total number of observations}}
Proportions answer the question: what share?
Because proportions are standardized, they allow us to compare groups of very different sizes.
R)n_pct() function from the ssstats package to describe the frequency and proportion of our categorical data.dataset_name is the name of the dataset you are working with.
n_pct() returns the count (n) and percentage for each category of the variable you list.
Just like mean_median(), we can use group_by() to get counts and percentages broken down by another categorical variable.
Note: if you need help getting started in R, please see the R module on Canvas.
department variable in the zootopia dataset.This tells us that of the 30 recruits,
Bar chart
A bar chart shows one bar per category, with the height of the bar equal to the number (or percentage) of observations in that category.
R)Bar chart
A bar chart shows one bar per category, with the height of the bar equal to the number (or percentage) of observations in that category.
dataset_name is the name of the dataset you are working with.
ggplot() is the function that creates a plot. The aes() function inside it tells R which variable to plot.
geom_bar() counts the observations in each category and draws a bar for each — you don’t need to calculate the counts yourself.
dataset_name |>
ggplot(aes(x = category_name,
y = after_stat(count) / sum(after_stat(count)))) +
geom_bar() +
scale_y_continuous(labels = scales::percent)dataset_name is the name of the dataset you are working with.
after_stat(count) / sum(after_stat(count)) converts the raw counts into a proportion of the total.
geom_bar() creates a bar graph.
scale_y_continuous(labels = scales::percent) relabels the y-axis as a percentage instead of a decimal.
Contingency Table
A two-way table shows how two categorical variables relate to one another, by counting observations at every combination of categories.
So far we’ve described one categorical variable at a time.
Just like a scatterplot let us compare two continuous variables, a contingency table lets us compare two categorical variables.
R)n_pct().dataset_name is the name of the dataset you are working with.
n_pct() constructs an R x C contingency table.
n_pct() automatically returns the column percentage.Let’s describe the characteristics of the recruits in the zootopia dataset.
Department
District
Academy status
We have covered basics for describing and visualizing categorical variables.