توضیح 1
The live starter code is self-contained and uses only runner-compatible functions.
Loading
درس 3 از 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.
توضیح 1
The live starter code is self-contained and uses only runner-compatible functions.
توضیح 2
Book-only outputs are labelled when the separate source dataset or advanced package was not supplied.
توضیح 3
R code, paths, function names, and formulas remain left-to-right in every locale.
اصطلاحات
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
نمادگذاری و فرمولها
unique key condition: anyDuplicated(key) = 0
A join is trustworthy only after both sides' key relationships are checked.
مثال حلشده
سناریو
Can a small monthly table be uniquely identified and grouped into ordered eras?
بررسی با R
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
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.
تمرین هدایتشده
سناریو
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.
توضیح 1
Extract year and month labels from a Date.
توضیح 2
Create a 12-row month lookup and validate its key.
توضیح 3
Explain many-to-many join expansion.
Selected solutions
These are compact solution routes. Confirm dimensions, units, and any changed modelling choices.
توضیح 1
format(d$date, '%Y') and format(d$date, '%b')
توضیح 2
calendar <- data.frame(month_number=1:12, month_label=month.abb); stopifnot(!anyDuplicated(calendar$month_number))
توضیح 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.
توضیح 1
Data frames combine equal-length columns.
توضیح 2
Factors preserve category order.
توضیح 3
Dates should be parsed, not left as display text.
توضیح 4
Joins need explicit validated keys.
اصطلاحات
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.
منبع
The authoritative 125-page English PDF accompanies this native lesson.
دریافتبازاندیشی
Name the assumption, evidence you would seek, and how the recommendation might change.
ارزیابی پایانی