Objective. Students use tidyverse packages (dplyr, tidyr, stringr) in R to group, aggregate, reshape, and summarize the same dataset used in the pandas lesson, and compare findings between the two language outputs.
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
In the previous lesson you explored a dataset using pandas in Python. Now you will perform the same exploration in R using the tidyverse — a collection of packages designed around a consistent philosophy of tidy data. By the end of this lesson you will be able to group and summarize data with dplyr, clean messy text with stringr, and reshape tables with tidyr. Comparing your R results to the pandas output you already produced is a built-in accuracy check: if the numbers agree (within rounding caused by floating-point differences), both analyses are likely correct.
The tidyverse packages share a common grammar and pipe-friendly design. A pipe (`|>` in base R 4.1+ or `%>%` from magrittr) passes the output of one function directly into the next, making code read like a series of English instructions. This reduces the need for intermediate variables and makes the data-transformation story easier to audit — an important quality in professional analytics work.
The two workhorses are `group_by()` and `summarize()` (also spelled `summarise()`). `group_by()` tags each row with its group membership without changing the data; `summarize()` then collapses each group into a single row of statistics.
```r
library(tidyverse)
df <- read_csv("lesson6_dataset.csv")
summary_tbl <- df |>
group_by(region) |>
summarize(
n = n(),
mean_sales = mean(sales, na.rm = TRUE),
med_sales = median(sales, na.rm = TRUE)
) |>
arrange(desc(mean_sales))
write_csv(summary_tbl, "region_summary.csv")
```
Key points:
When you compare these results to your pandas output, minor decimal differences (e.g., 1234.56 vs. 1234.5600000001) are acceptable; they arise from floating-point arithmetic and do not indicate an error. Larger discrepancies should be investigated — a common cause is one tool dropping NAs while the other includes them.
Real datasets often contain inconsistent formatting: city names like `"New York"`, `"new york"`, and `" New York "` are all different strings to a computer. The stringr package provides a predictable, pipe-friendly interface to string operations.
| Function | Purpose |
|---|---|
| `str_to_lower()` | Convert all characters to lowercase |
| `str_trim()` | Remove leading and trailing whitespace |
| `str_replace_all()` | Replace a pattern with a new string |
```r
before_cities <- n_distinct(df$city)
df <- df |>
mutate(
city = str_trim(city),
city = str_to_lower(city)
)
after_cities <- n_distinct(df$city)
cat("Unique city values before:", before_cities, "\n")
cat("Unique city values after: ", after_cities, "\n")
```
Record both counts in your lab log. A decrease in distinct values after cleaning confirms that duplicates caused by formatting differences have been merged. This documentation is important for reproducibility: a future analyst (or your future self) can see exactly what changed and why.
Data comes in two shapes. Wide format places each variable in its own column — convenient for reading but awkward for many ggplot2 visualizations and statistical models. Long format stacks variable names into one column and their values into another — ideal for grouped operations and plotting.
```r
long_tbl <- summary_tbl |>
pivot_longer(
cols = c(mean_sales, med_sales),
names_to = "metric",
values_to = "value"
)
wide_again <- long_tbl |>
pivot_wider(
names_from = "metric",
values_from = "value"
)
stopifnot(nrow(wide_again) == nrow(summary_tbl))
stopifnot(ncol(wide_again) == ncol(summary_tbl))
```
The `stopifnot()` calls act as automatic checks: if row or column counts do not match, R stops and reports an error so you catch the problem immediately.
Your deliverable is an R Markdown (`.Rmd`) file that knits to HTML or PDF without errors. Each code chunk should be followed by a plain-English explanation. For the reshape section, write at least one sentence explaining when long format is preferable (modeling, plotting) and when wide format is preferable (display, export to spreadsheets).
Before submitting, knit the document and confirm it completes without errors. A clean knit is the R equivalent of a Python script that runs from top to bottom without exceptions.
Although this lesson involves no electrical equipment or chemicals, professional data work carries its own integrity obligations. Never alter the original source file; always read from it and write outputs to new files. Keep your lab log entries factual — record what the data showed, not what you hoped it would show. Misrepresenting data quality or cleaning steps is an ethical violation with real consequences in business and public-sector analytics.
Answer each question, then check it. Nothing is recorded; this is your own practice.
A free tech program — complimentary, as our commitment to get America working. Help us build VoTechWorks. Tell us what works, what doesn’t, and what you want to learn.
Does the school offer financial aid?
No. We don’t offer financial aid because we’re free. No, really: completely free.
Do you offer industry certification?
No. We are not certified yet, so we can’t offer that to students. What we do offer is knowledge and understanding of a trade.
Enroll first in the trade you’re interested in. You get your key, your program opens, and you begin studying.
Enroll free →