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 anything looks unusual.

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() reports an object’s storage mode; it does not calculate the statistical mode.
  • The R package, modeest, provides the mfw(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.

4️⃣ Quartiles divide sorted values into four parts

Lowest 25%
→ Q₁
Next 25%
→ Q₂
Next 25%
→ Q₃
Highest 25%
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\); that span is the IQR. The line inside is the median.
  • In R’s standard boxplot, whiskers reach the farthest observed values within \(1.5 \times \mathrm{IQR}\) of the box.
  • Points beyond the whiskers are flagged as possible outliers to investigate—not automatic errors.

✂️ Selecting Values from a Vector

🔎 Square brackets select values from a vector

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 selecting part of an object. In R, square brackets [] hold the selection rule.
  • 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 selects several values

my_vector[c(3, 4, 5)]   # 30 40 50
my_vector[3:5]          # 30 40 50
102030405060
  • Put positions inside c(...) to select 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
10
FALSE
20
FALSE
30
FALSE
40
TRUE
50
TRUE
60
TRUE
  • The comparison creates one TRUE or FALSE for each value; brackets keep the TRUE positions.

🧪 Classwork 3: Put the pieces together

Create vectors Calculate summaries Select values