Explanation 1
The live starter code is self-contained and uses only runner-compatible functions.
Loading
Lesson 6 of 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.
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
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
Notation and formulas
group summary = one statistic per declared group
Grouping is analytical state; drop it deliberately after summarising.
Worked example
Scenario
How do average inflation and variability differ across analyst-defined eras?
R check
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 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 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.
Guided practice
Scenario
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.
Explanation 1
Return the largest absolute changes.
Explanation 2
Apply the same mean function to numeric columns.
Explanation 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.
Explanation 1
Order by -abs(c(NA,diff(x))) and inspect the first rows.
Explanation 2
summarise(across(where(is.numeric), mean)) in dplyr.
Explanation 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.
Explanation 1
Verbs express table operations.
Explanation 2
Grouping changes calculation scope.
Explanation 3
Pipelines should remain inspectable.
Explanation 4
Joins require explicit keys and relationship checks.
Terminology
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.
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%.