Lecture 4

Descriptive Statistics with R

Byeong-Hak Choe

SUNY Geneseo

September 14, 2026

📊 Descriptive Statistics

🔎 Descriptive statistics turn data into summaries

Ten grocery bills ($)
23, 45, 12, 67, 89,
34, 56, 78, 90, 43
Mean53.7
Median50.5
Range78
  • Descriptive statistics organize and summarize the values in a dataset.
    • They help us see what is typical, how much values vary, and whether some values are unusually far from the rest.

Note

They describe what is in the data. By themselves, they do not explain why a pattern occurred or predict what will happen next.

🛠️ Summaries answer practical questions

Question Grocery-bill example
Can we trust the data? Flag a $10,000 bill when most bills are $50–$150.
What is typical? Compare the mean and median bill.
How much do values vary? Start with the range; later, compare other measures of spread.
Could one unusual bill distort the summary? Compare the mean and median, then inspect the distribution.

Tip

A summary starts the investigation; it does not replace the original data.

🎯 Choosing a measure of “typical”

Measure Meaning of “typical” Best suited to Important feature
Mean Balance point Numeric values Uses every value
Median Middle after sorting Numeric values Resists extreme values
Mode Most frequent value Numeric or categorical values May tie or not be unique
  • The most useful measure depends on the data and the question—not on one universal rule.

➗ The mean shares a total equally

\[ \bar{x} = \frac{x_{1} + x_{2} + \cdots + x_{n}}{n} \]

x <- c(60, 70, 80, 90, 100)
sum(x) / length(x)   # 80
mean(x)               # 80
  • \(x_i\) represents observation \(i\), and \(n\) is the number of observations.
  • The mean is the sum divided by the number of observations; mean(x) calculates it directly.

Note

Because the mean uses every value, one extreme value can shift it substantially.

⚖️ A weighted mean lets some values count more

\[ \bar{x}_{w} = \frac{w_{1}x_{1} + w_{2}x_{2} + \cdots + w_{n}x_{n}}{w_{1} + w_{2} + \cdots + w_{n}} \]

Course component Score Weight
Homework 90 40%
Exam 80 60%
Course average90 × .40 + 80 × .6084
weighted.mean(c(90, 80), w = c(.40, .60))  # 84
  • An ordinary mean gives each value equal weight; a weighted mean lets contributions differ.

📍 The median is the center of a sorted vector

For a numeric vector sorted from smallest to largest, \(x_{(1)} \le x_{(2)} \le \cdots \le x_{(n)}\),

\[ \operatorname{median}(x)= \begin{cases} x_{\left(\frac{n+1}{2}\right)}, & n \text{ is odd},\\[4pt] \dfrac{x_{\left(\frac{n}{2}\right)}+x_{\left(\frac{n}{2}+1\right)}}{2}, & n \text{ is even}. \end{cases} \]

Odd number of values
1, 3, 3, 6, 7, 8, 9
Median = 6
Even number of values
1, 2, 3, 4, 5, 6, 8, 9
Median = (4 + 5) / 2 = 4.5
x <- c(60, 70, 80, 90, 100)
median(x)   # 80
  • The median is less sensitive to extreme values than the mean.

🔁 Mode: the most common value

response <- c("Online", "In person", "Online",
              "Hybrid", "Online")
table(response)
Online
3
Hybrid
1
In person
1
  • Here, Online is the mode. Mode is useful for categories or repeated discrete values; ties or no unique mode are possible.

Warning

  • Base R’s mode() does not calculate the statistical mode.
  • The R package, modeest, provides the mfv(x) function that calculate the mode of values in vector x.

↔︎️ The same mean can hide very different spread

Tightly grouped
78 · 79 · 80 · 81 · 82
mean = 80
Widely spread
60 · 70 · 80 · 90 · 100
mean = 80
  • Dispersion (or variability) describes how far values are spread apart.
  • We will compare three approaches: range, variance and standard deviation, and the interquartile range (IQR).

📏 Range uses only the smallest and largest values

\[ \text{range width} = \max(x)-\min(x) \]

60
minimum
100
maximum
x <- c(60, 70, 80, 90, 100)
range(x)                  # 60 100: the endpoints
x_range <- max(x) - min(x) # 40: the width

Note

The range is highly sensitive to extremes: replacing 100 with 200 changes the width from 40 to 140.

📐 Variance uses squared distances from the mean

\[ s^2=\frac{\sum_{i=1}^{n}(x_i-\bar{x})^2}{n-1} \]

60 70 80 90 100
Deviation from mean 80 −20 −10 0 10 20
Squared deviation 400 100 0 100 400
var(x)   # 1000 / (5 - 1) = 250
  • R’s var(x) calculates the sample variance, using \(n-1\) in the denominator.
  • Squaring prevents negative and positive deviations from canceling, but produces squared units.

📊 Standard deviation restores the original unit

\[ s=\sqrt{s^2} \]

250 points²variance
→
square rootundo the square
→
15.8 pointsstandard deviation
sd(x)   # 15.81139
  • A larger standard deviation means more spread when comparing the same variable on the same scale.
Smaller s: closer to the mean
78 · 79 · 80 · 81 · 82
mean = 80 · s ≈ 1.6
Larger s: farther from the mean
60 · 70 · 80 · 90 · 100
mean = 80 · s ≈ 15.8

🎚️ Drag \(s\) to change the data’s spread

s = 1.5
Scattered observations with adjustable standard deviation Thirty observations are scattered around a mean of 5. The dots move to a new non-overlapping arrangement when the standard deviation changes; vertical position is visual jitter only. Dotted guides mark 3.5, 5, and 6.5: the mean minus one standard deviation, the mean, and the mean plus one standard deviation.
Meanx̄ = 5
Standard deviations = 1.5
Variances² = 2.25

The observations have mean 5, standard deviation 1.5, and variance 2.25. The dots re-scatter horizontally and vertically at each setting, and the dotted outer guides are at 3.5 and 6.5.

4️⃣ Quartiles divide sorted values into four parts

x <- c(60, 70, 80, 90, 100)
quantile(x, c(.25, .50, .75))   # 70 80 90
  • \(Q_1\), \(Q_2\), and \(Q_3\) are cut points in a sorted vector; about 25%, 50%, and 75% of values lie at or below them.
  • \(Q_2\) is the median. Quartiles locate the cuts; the IQR measures the spread between \(Q_1\) and \(Q_3\).

📦 The IQR measures the spread of the middle 50%

\(\mathrm{IQR}=Q_3-Q_1\)
Lower 25%
Middle 50% IQR
Upper 25%
IQR(x)   # 20
  • The IQR measures how spread out the middle half of the values are and is less affected by extremes than the range.
  • A small IQR means the middle half is tightly grouped; a large IQR means it is more spread out.

📦 A boxplot shows quartiles and possible outliers

  • The box spans \(Q_1\) to \(Q_3\), which is the IQR. The line inside is the median.
  • The fences are \(Q_1 - 1.5 \times \mathrm{IQR}\) and \(Q_3 + 1.5 \times \mathrm{IQR}\). Whiskers stop at the farthest observed values inside them.
  • Here, the lower whisker does not reach the lower fence, so it is shorter than the upper whisker. Only the point beyond the upper fence is a possible outlier.

🧪 Classwork 3: Descriptive Statistics

Calculate summaries Interpret center and spread Work with quartiles and IQR

✂️ Filtering a Vector: Keeping Values You Want

🔎 Square brackets keep values by position or condition

scores <- c(72, 85, 79, 91, 88)
scores[2]one position
85
scores[c(2, 5)]several positions
85, 88
scores[scores >= 85]values meeting a condition
85, 91, 88
  • Subsetting means keeping part of an object. With a vector, square brackets [] contain the positions or logical condition that determine which values are kept.
  • With a logical condition, filtering keeps values at TRUE positions and omits values at FALSE positions.
  • Vector positions begin at 1.

📍 R counts vector positions from 1

Position 1 2 3 4 5 6
Value 10 20 30 40 50 60
my_vector <- c(10, 20, 30, 40, 50, 60)
my_vector[2]   # 20
  • A position is a whole number beginning at 1.
  • my_vector[2] means “the value in position 2,” not “the value 2.”

🔢 A vector of positions keeps several values

my_vector[c(3, 4, 5)]   # 30 40 50
my_vector[3:5]          # 30 40 50
  • Put positions inside c(...) to keep several values.
  • 3:5 is shorthand for the consecutive positions c(3, 4, 5).

✅ Logical indexing keeps values marked TRUE

keep <- my_vector >= 40
keep                       # FALSE FALSE FALSE TRUE TRUE TRUE
my_vector[keep]            # 40 50 60

my_vector[my_vector >= 40] # 40 50 60
  • The comparison creates one TRUE or FALSE for each value. Filtering keeps the values at TRUE positions and omits the values at FALSE positions.

🧪 Classwork 4: Filtering a Vector

Filter by condition Keep named positions Interpret TRUE and FALSE