Explanation 1
The live starter code is self-contained and uses only runner-compatible functions.
Loading
Lesson 3 of 22
Build rectangular data, encode categories, parse dates, and validate keys before joins.
Motivation
A table becomes analytically reliable only when one row has a declared meaning and the key identifying that row is unique.
Why this matters
Build rectangular data, encode categories, parse dates, and validate keys before joins.
Packages and data
Base R; dplyr is optional for later joins.
Explanation 1
The live starter code is self-contained and uses only runner-compatible functions.
Explanation 2
Book-only outputs are labelled when the separate source dataset or advanced package was not supplied.
Explanation 3
R code, paths, function names, and formulas remain left-to-right in every locale.
Terminology
What one row represents.
One month in Canada
Categorical values with declared levels and optional order.
low < medium < high
Column set intended to identify each row uniquely.
date for a monthly series
Notation and formulas
unique key condition: anyDuplicated(key) = 0
A join is trustworthy only after both sides' key relationships are checked.
Worked example
Scenario
Can a small monthly table be uniquely identified and grouped into ordered eras?
R check
d <- data.frame(date = as.Date(c('2022-01-01','2022-02-01','2023-01-01','2023-02-01')), value = c(4.8, 5.1, 3.2, 2.9))
d$era <- factor(ifelse(d$date < as.Date('2023-01-01'), 'Earlier', 'Later'), levels = c('Earlier','Later'), ordered = TRUE)
print(str(d))
print(anyDuplicated(d$date))
print(table(d$era))The explicit date key and ordered era definition make sorting and grouping auditable. Limitation: Era boundaries are analyst choices and do not establish causal regimes.
Visual
Two observations appear in each declared era.
R connection
Run the self-contained starter code in the protected STATLAB R runner. The code prints an auditable result and avoids network or unrestricted file access.
Live R Lab
Run a self-contained example, verify its output, and explain one limitation for data frames, factors, and dates.
Ready to run
Common mistake
Row names are not stable primary keys and can change after sorting or export.
STATLAB Tip
Be able to finish the sentence: one row represents …, then validate the identifying columns.
Guided practice
Scenario
Create ordered inflation bands below 1%, 1–under 3%, 3–under 5%, and 5% or more.
Compare your result with the definition, units, and model assumptions—not only with a target number.
Exercises
Complete these without looking at the selected solutions. More than one defensible program may exist.
Explanation 1
Extract year and month labels from a Date.
Explanation 2
Create a 12-row month lookup and validate its key.
Explanation 3
Explain many-to-many join expansion.
Selected solutions
These are compact solution routes. Confirm dimensions, units, and any changed modelling choices.
Explanation 1
format(d$date, '%Y') and format(d$date, '%b')
Explanation 2
calendar <- data.frame(month_number=1:12, month_label=month.abb); stopifnot(!anyDuplicated(calendar$month_number))
Explanation 3
Repeated keys on both sides create every matching combination and can multiply rows.
Chapter summary
Build rectangular data, encode categories, parse dates, and validate keys before joins.
Explanation 1
Data frames combine equal-length columns.
Explanation 2
Factors preserve category order.
Explanation 3
Dates should be parsed, not left as display text.
Explanation 4
Joins need explicit validated keys.
Terminology
construct table
Use in Data Frames, Factors, and Dates.
encode categories
Use in Data Frames, Factors, and Dates.
parse dates
Use in Data Frames, Factors, and Dates.
validate keys
Use in Data Frames, Factors, and Dates.
References and provenance
Safavi (2026), Chapter 3. Student notes: Mohammad Safavi, Ph.D., STATLAB Academy, Version 1.0.
Resource
The authoritative 125-page English PDF accompanies this native lesson.
DownloadReflection
Name the assumption, evidence you would seek, and how the recommendation might change.
Exit check
Reach 70% to complete the chapter. Explanations appear after submission.
Checkpoint
Question 1 of 2. Answered 0/2. Passing score: 70%.