Numerical and Categorical Variable Concepts
Trent University
Chapters 4-6 in Introduction to Modern Statistics (2e).
I was able to download the data from our Zoom poll:
# A tibble: 6 × 8
`#` `User Name` `Email Address` `Submitted Date and Time` `Collected from`
<dbl> <chr> <chr> <dttm> <chr>
1 1 Anonymous Anonymous 2026-09-21 14:28:00 Meeting
2 2 Anonymous Anonymous 2026-09-21 14:28:00 Meeting
3 3 Anonymous Anonymous 2026-09-21 14:28:00 Meeting
4 4 Anonymous Anonymous 2026-09-21 14:28:00 Meeting
5 5 Anonymous Anonymous 2026-09-21 14:28:00 Meeting
6 6 Anonymous Anonymous 2026-09-21 14:28:00 Meeting
# ℹ 3 more variables: `Topic/Name` <chr>, `Meeting/Webinar ID` <dbl>,
# `How many piercings do you have?` <dbl>
The vector of data looks like:
[1] 2 2 0 4 3 5 0 1 1 4 1 4 0 4 2 2 1 4 2 2 0 6 4 4 2
[26] 3 2 7 3 0 4 1 2 1 0 1 4 0 2 0 1 0 2 2 2 1 2 0 1 0
[51] 0 0 4 5 0 2 1 2 1 0 10 2 0 2 4 1 4 3 10 0 6 4 2 1 1
[76] 2 9 6 5 3 8 1 0 2 6 3 5 1 9 6 1 3 3 4 0 5 0 3 0 0
[101] 2 3 4 5 6 10 2 2 2 2 2 4 0 0 0 1 1 5 4 0 0 3 0 4 0
[126] 5 0 1 3 4 0 1 6 3 1 3 2 3 6 1 1
We can turn the data into a histogram:

I’ll show you how to make one of these next week.
We can calculate the mean, variance, and standard deviation
[1] 2.503546
[1] 5.39463
[1] 2.322634
The “average” student has 2.5 piercings…
The median is the value that splits the data in half when ordered in ascending order.

If there are an even number of observations, then the median is the average of the two values in the middle.

Since the median is the midpoint of the data, 50% of the values are below it. Hence, it is also the 50th percentile.
Medians can’t really be computed with calculators, but we’ll use R for this anyway …
For our piercing data:
The median, 2, is the number that splits the class in half – 50% have two or fewer piercings and 50% have two or more
A percentile is the the smallest value from an ordered list of numbers which is greater than or equal to that percentage of list elements.
Example: The \(42^\text{nd}\) percentile of the numbers \(\{0, 1, 2, 3, \cdots, 99, 100 \}\) is 42.
Note
The 0 in the example is needed to make the quantile calculations “work”. If you were to try:
quantile(0:100, probs = 0.42)
then you would get 42 as the result.
Between Q1 and Q3 is the middle 50% of the data. The range these data span is called the interquartile range, or the IQR.
\[ \text{IQR} = \text{Q3} - \text{Q1} \]
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 |
| 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
| 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 3 | 3 | 3 | 3 | 3 | 3 |
| 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 4 | 4 | 4 | 4 | 4 | 4 |
| 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 5 | 5 |
| 5 | 5 | 5 | 5 | 5 | 5 | 6 | 6 | 6 | 6 | 6 | 6 | 6 | 6 | 7 |
| 8 | 9 | 9 | 10 | 10 | 10 | NA | NA | NA | NA | NA | NA | NA | NA | NA |
| First 70 Ordered Observations |
| Median |
| Last 70 Ordered Observations |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 |
| 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
| 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 3 | 3 | 3 | 3 | 3 | 3 |
| 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 4 | 4 | 4 | 4 | 4 | 4 |
| 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 5 | 5 |
| 5 | 5 | 5 | 5 | 5 | 5 | 6 | 6 | 6 | 6 | 6 | 6 | 6 | 6 | 7 |
| 8 | 9 | 9 | 10 | 10 | 10 | NA | NA | NA | NA | NA | NA | NA | NA | NA |
| First Quarter | Third Quarter |
| Second Quarter | Fourth Quarter |
Quantiles are actually hard to do with calculators, but R makes it easy.
This computes the 40th and 60th percentiles of the vector, x, created by randomly sampling the values from 1 to 100 with replacement.
25% 75%
11.75 71.50
[1] 59.75
OR, even more straight forward:
We introduced histograms earlier as our first numerical summary plot. Let’s see what else people have developed as tools for visualizing and exploring data.
Box plots were developed by John Tukey in 1970, and popularized in his book Exploratory Data Analysis in 1977. The box in a box plot represents the middle 50% of the data, and the thick line in the box is the median.


Let’s make a boxplot for our piercing example:

The whiskers of a box plot can extend up to \(1.5 \times \text{IQR}\) away from the quartiles.
Example: IQR: \(4- 1 = 3\)
A potential outlier is defined as an observation beyond the maximum reach of the whiskers. It is an observation that appears extreme relative to the rest of the data.
The whisker stops at the smallest and largest values within 1.5 IQR of the quartiles.
For our dataset, that’s 0 and 8.

Why is it important to look for outliers?
Important
Skew is something we are going to be checking once we start statistical inference later in the course.
Median and IQR are more robust to skewness and outliers than mean and SD. Therefore,
If you would like to estimate the typical household income for a student, would you be more interested in the mean or median income?
Median and IQR are more robust to skewness and outliers than mean and SD. Therefore,
If you would like to estimate the typical household income for a student, would you be more interested in the mean or median income?
Median
If the distribution is symmetric, centre is often defined as the mean:

If the distribution is skewed or has extreme outliers, centre is often defined as the median

Which is most likely true for the distribution of percentage of time actually spent taking notes in class versus browsing your favourite social media feed?

Which is most likely true for the distribution of percentage of time actually spent taking notes in class versus browsing your favourite social media feed?
If we compute, the mean = 76% and the median = 80%. So …

Scatterplots are useful for visualizing the relationship between two numerical variables. They will be incredibly important and we will spend a lot of time making them, and analyzing them.

Do life expectancy and total fertility appear to be associated or independent?
They appear to be linearly and negatively associated: as fertility increases, life expectancy decreases.
Was the relationship the same throughout the years, or did it change?
The relationship changed over the years.
https://www.gapminder.org/tools/
A table that summarizes data for two categorical variables is called a contingency table.
The contingency table below shows the distribution of students’ degree programs and whether or not they are looking for a life partner while in university.
Looking for Life Partner
|
|||
|---|---|---|---|
| No | Yes | Total | |
| Program | |||
| Sciences | 86 | 51 | 137 |
| Humanities | 52 | 18 | 70 |
| Total | |||
| 138 | 69 | 207 | |
A bar plot is a common way to display a single categorical variable. A bar plot where proportions instead of frequencies are shown is called a relative frequency bar plot.

The Main Point
Both bar plots and histograms are counting the number of observations.
However, bar plots are for categorical data, and histograms are for numerical data.
Does there appear to be a relationship between a student’s major and whether the student is looking for a life partner in university?
Looking for Life Partner
|
|||
|---|---|---|---|
| No | Yes | Total | |
| Major | |||
| Sciences | 86 | 51 | 137 |
| Humanities | 52 | 18 | 70 |
| Total | |||
| 138 | 69 | 207 | |
To answer this question we examine the row proportions:
What are the differences between the three visualizations shown below?

Can you tell which order encompasses the lowest percentage of mammal species1?

Does there appear to be a relationship between class year and the number of clubs students are in?

Visualizing your data using an appropriate choice of plot is recommended as the first thing you do with a data set, regardless of your objective. The human eye is very, very good at seeing patterns and detecting things that do not belong.
However, if you use the wrong visualization, it can be either useless, or actively deceiving.
We will practice visualization-making in R during the Labs.
