توضیح 1
The live starter code is self-contained and uses only runner-compatible functions.
Loading
درس 6 از 22
Use a small grammar of verbs for readable row, column, grouping, and join operations.
Motivation
A consistent transformation grammar lets reviewers focus on analytical choices instead of decoding ad hoc indexing.
Why this matters
Use a small grammar of verbs for readable row, column, grouping, and join operations.
Packages and data
dplyr 1.2.1 was used for the book; the live lab demonstrates equivalent base-R grouping so it remains runner-compatible.
توضیح 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.
اصطلاحات
A named table transformation.
filter keeps rows; select keeps columns
Changes the scope of later summaries.
one mean per era
Expected key cardinality across two tables.
many-to-one metadata join
نمادگذاری و فرمولها
group summary = one statistic per declared group
Grouping is analytical state; drop it deliberately after summarising.
مثال حلشده
سناریو
How do average inflation and variability differ across analyst-defined eras?
بررسی با R
d <- data.frame(era=rep(c('Earlier','Later'), each=3), inflation=c(1.8,2.2,2.6,4.8,3.7,2.9))
result <- aggregate(inflation ~ era, d, function(x) c(mean=mean(x), sd=sd(x)))
print(result)The grouped summaries describe this extract and depend on the chosen era boundary. Limitation: Serial dependence and unequal periods would invalidate a simple independent-group interpretation in real monthly data.
Visual
Mean inflation is higher in the later illustrative 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 transforming data with dplyr.
Ready to run
Common mistake
filter selects rows; select chooses columns. Hidden grouping can also change later calculations.
STATLAB Tip
Name intermediate tables for what they represent, not temp1 or step2.
تمرین هدایتشده
سناریو
Create an inflation gap relative to 2%, then summarize its mean, median, minimum, and maximum by era.
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
Return the largest absolute changes.
توضیح 2
Apply the same mean function to numeric columns.
توضیح 3
Join a tiny year metadata table and check unmatched keys.
Selected solutions
These are compact solution routes. Confirm dimensions, units, and any changed modelling choices.
توضیح 1
Order by -abs(c(NA,diff(x))) and inspect the first rows.
توضیح 2
summarise(across(where(is.numeric), mean)) in dplyr.
توضیح 3
Use anti_join checks and compare row counts before and after.
Chapter summary
Use a small grammar of verbs for readable row, column, grouping, and join operations.
توضیح 1
Verbs express table operations.
توضیح 2
Grouping changes calculation scope.
توضیح 3
Pipelines should remain inspectable.
توضیح 4
Joins require explicit keys and relationship checks.
اصطلاحات
keep rows
Use in Transforming Data with dplyr.
create columns
Use in Transforming Data with dplyr.
reduce rows
Use in Transforming Data with dplyr.
add matched columns
Use in Transforming Data with dplyr.
References and provenance
Safavi (2026), Chapter 6; dplyr documentation. 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.
ارزیابی پایانی