Quality Matters Alignment: This assignment addresses QM Standards 2.1, 3.1, 4.1, and 5.2. Learning objectives are listed at the top of each Part. The rubric at the end provides grading criteria. Feedback will be returned within one week of the submission deadline.


Assignment Overview

Due: Before the start of next class
Submission: Rename this file LastName_FirstName_NSCI253_HW_Wk11.Rmd and submit to Google Classroom.
Total points: 50

This homework covers two foundational skills from Week 11: communicating results with R Markdown (Module 14) and Troubleshooting Strategies (Module 15).

Submission requirements:

  • Submit a single .Rmd file that knits without errors to HTML.
  • Complete all tasks within this file using the provided headings — do not remove them.
  • Place code inside labeled chunks using only letters, numbers, and hyphens (e.g., challenge-a-fix).
  • Written responses go in Markdown body text, not inside chunks.
  • A document that fails to knit receives zero points for any task that prevents knitting.

Academic integrity: You may use ?help, r4wrds materials, and LLM tools as diagnostic aids. If you use an LLM, briefly note what prompt you used and what you verified independently. All explanations must be your own.


Learning Objectives

By the end of this assignment, you will be able to:

  1. Identify and describe the three components of an R Markdown document (YAML header, body text, code chunks) and explain the role each plays.
  2. Knit a .Rmd file to HTML and interpret the relationship between source and output.
  3. Diagnose common R errors using error messages, class(), and ?function.
  4. Write a minimal reproducible example (reprex) with packages, data, code, and a descriptive comment.
  5. Apply a systematic six-step debugging workflow to fix errors in R code.

1 Part 1: R Markdown Document Structure (15 points)

Learning objectives: LO 1, LO 2


1.1 Task 1.1 — Annotated YAML Header (5 points)

Look at the YAML header at the top of this file (between the --- lines). Just below this heading, add a Markdown comment block (<!-- ... -->) that explains in your own words what each of the following fields does:

  • title:
  • output: html_document:
  • toc: true
  • error: TRUE > Tip: Try changing theme: from cosmo to journal, knit, and > see what changes. Other options: flatly, readable, spacelab.

1.2 Task 1.2 — Three Components in Practice (5 points)

Demonstrate each of the three R Markdown components directly below this heading:

  1. YAML header — annotated in Task 1.1.
  2. Body text — Write a paragraph (3–5 sentences) using at least three formatting elements: a heading, bold text, and a hyperlink. Describe what R Markdown is and why reproducibility matters in science.
  3. Code chunk — Add a labeled chunk (```{r my-first-chunk}) that creates a numeric vector of at least 5 environmental measurements (e.g., stream depths, temperatures), calculates its mean(), and uses at least one chunk option (echo, eval, or include). Write one sentence above the chunk explaining the option you chose.

measurements <- c(12.3, 14.1, 13.8, 15.0, 12.9) mean(measurements)


1.3 Task 1.3 — Knit and Reflect (5 points)

Knit your document, then answer these questions in complete sentences in the space below:

  • How does the knitted HTML compare to the .Rmd source? Name one similarity and one difference.
  • What chunk option did you use, and what effect did it have?
  • What would change if you set toc: false? Predict first, then test it.

HTML will hide the raw markdown syntax and show the formatted headings, bold text, and code output in a clear layout echo = TRUE was used. This caused the code to appear in the knitted document with the output changing toc: false, the table of contents would fail to appear in the HTML output. I tested it, I seen the sidebar disappeared with the document no longer showed section links. —

2 Part 2: Debugging the Challenge Exercises (20 points)

Learning objectives: LO 3, LO 5

For each challenge below:

  1. Leave the original broken code in its error=TRUE chunk as-is.
  2. Add a second chunk labeled challenge-X-fix with your corrected code.
  3. In the Markdown space between the two chunks, explain: (a) what the error was, (b) which debugging step you used (READ / CHECK / OPEN / SIMPLIFY / SEARCH / ASK), and (c) how your fix resolves the problem.

2.1 Challenge A — Case-Sensitive Object Name (5 points)

# Challenge A: what is wrong here?
scores <- c(85, 92, 78, 90, 88)
sum(Scores)
## Error:
## ! object 'Scores' not found

Scores was capitalized is why the error happened. We all know R is case-sensitive. I carefully but noticed right away that the Scores was out of place, this is called READ by using lower case object name sum()

# STUDENT: paste your corrected code here
scores <- c(85, 92, 78, 90, 88)
sum(scores)
## [1] 433

2.2 Challenge B — NA Propagation in mean() (5 points)

# Challenge B: why does this return NA?
temps <- c(72, 75, NA, 68, 71)
mean(temps)
## [1] NA
# Hint: check ?mean -- look at the Arguments section carefully

The error was mean(temps) returning NA due to the reason the vector containing an NA and mean() propagates missing values Using CHECK, looking at mean and reading Arguments section adding na.rm = tells R to get rid of missing values before calculating the mean

# STUDENT: paste your corrected code here
```temps <- c(72, 75, NA, 68, 71) 
mean(temps, na.rm = TRUE)   
## Error in parse(text = input): attempt to use zero-length variable name

2.3 Challenge C — Date Class and max() (5 points)

# Challenge C: does this give the right answer?
dates_chr <- c("2023-01-15", "2023-06-30", "2023-12-01")
max(dates_chr)              # seems to work... is it correct?
## [1] "2023-12-01"
max(as.Date(dates_chr))     # what changed and why does it matter?
## [1] "2023-12-01"

look at max(dates_chr) this will compare dates as character strings but not the actual dates. Just like before, READ was used as the debugging step You convert to Date class making R compare the underlying date values making sure the result is the most recent date.

# STUDENT: paste your corrected / clarified code here
dates_chr <- c("2023-01-15", "2023-06-30", "2023-12-01")
dates_date <- as.Date(dates_chr)
max(dates_date)
## [1] "2023-12-01"

2.4 Challenge D — Join Key Mismatch (5 points)

# Challenge D: diagnose this join error
df1 <- tibble(id = 1:3, value = c("a", "b", "c"))
df2 <- tibble(ID = 1:3, score = c(10, 20, 30))  # note: "ID" not "id"
left_join(df1, df2)
## Error in `left_join()`:
## ! `by` must be supplied when `x` and `y` have no common variables.
## ℹ Use `cross_join()` to perform a cross-join.
# Check ?dplyr::left_join -- how do you specify the key column?

The is left_join() that fails due to df1 using id and df2 using ID. this causes R to struggle to find a shared column name. CHECK was used. The fix works by specifying by = c(“id”= “ID”) this will let R know which columns match

# STUDENT: paste your corrected code here
df1 <- tibble(id = 1:3, value = c("a", "b", "c"))
df2 <- tibble(ID = 1:3, score = c(10, 20, 30))

left_join(df1, df2, by = c("id" = "ID"))
## # A tibble: 3 × 3
##      id value score
##   <int> <chr> <dbl>
## 1     1 a        10
## 2     2 b        20
## 3     3 c        30

3 Part 3: Write a Reprex (10 points)

Learning objective: LO 4


3.1 Task 3.1 — Identify the Problem (3 points)

Choose one scenario. In the space below, describe what you expect the code to do and what actually happens. Run class() on the problematic object, include the output, and explain why that class causes an error.

  • Scenario A: A vector of water-quality readings stored as character strings; you want to calculate their mean().
  • Scenario B: Dates stored as plain text; you want the most recent one using max().

For this task, Scenario A was chosen, using the code to calculate the mean of readings, but it instead gave me a warning returning NA. I ran class() showing the values stored as “character” that stops numeric operations.

# STUDENT: use class() to inspect the problematic object here
readings <- c("5.2", "4.8", "6.1", "5.9", "5.0")
class(readings)
[1] "character"
## Error in parse(text = input): <text>:4:1: unexpected '['
## 3: class(readings)
## 4: [
##    ^

3.2 Task 3.2 — Write the Reprex (7 points)

Write a complete, self-contained reprex in the chunk below using Hadley Wickham’s four-component structure:

  1. Packages — all library() calls at the top.
  2. Data — a hand-made dataset of 5 rows or fewer using tibble() or c(). Do not reference any external file.
  3. Code — the minimal code that reproduces the error.
  4. Comment — describe expected vs. actual output at the end (e.g., # Expected: numeric mean; Got: warning + NA).

Then add a Markdown paragraph explaining the fix, followed by a second chunk showing the corrected code.

# STUDENT: four-component reprex here
# 1. Packages:    library(...)
# 2. Data:        my_data <- ...
# 3. Code:        ...
# 4. Comment:     # Expected: ___; Got: ___

This error will happen when the readings are stored as character strings. R will not like mean on a text as it cannot compute it which results in the function returning NA.

# STUDENT: corrected code here
# 1. Packages
library(tibble)

# 2. Data
readings <- c("5.2", "4.8", "6.1", "5.9", "5.0")

# 3. Code
mean(readings)
## [1] NA
# 4. Comment
# Expected: numeric mean of the five readings
# Got: warning + NA because readings are character strings

4 Part 4: Semester Project Start (5 points)

Learning objectives: LO 1, LO 2

Create a separate .Rmd file for your semester project following the project management format at https://r4wrds.netlify.app/intro/m_project_management.

Your project .Rmd must include at minimum:

  • A YAML header with title, author, date, and output: html_document.
  • One Level 1 heading reflecting your project topic.
  • One labeled code chunk (with at least one chunk option) that loads a package your project will use or performs a relevant operation.
  • A paragraph (3+ sentences) describing what your project will analyze.

Paste your project file’s YAML header and first section below. The full project file stays in your project folder.

4.1

title: “Pactola Watershed Water Budget” author: “Mike Yellow Boy” date: “2026-04-15” output: html_document: toc: true toc_float: true theme: cosmo —

# STUDENT: paste your project's first code chunk here
library(tidyverse)

Grading Rubric

QM Standards: 2.1, 3.1, 4.1, 5.2  ·  Total: 50 points

Task Criterion Exemplary (Full Credit) Proficient (Partial Credit) Developing (Minimal Credit) Points
Part 1 — R Markdown Structure (15 pts)
1.1 YAML Annotations All four fields annotated in the student’s own words with clear, accurate explanations; document knits to floating-TOC HTML. 3–4 fields annotated, or explanations partially accurate or close to course materials. Fewer than 3 fields annotated, or explanations missing or inaccurate. 5 / 3 / 1
1.2 Three Components YAML annotated; body text uses heading + bold + hyperlink; labeled chunk has environmental vector, correct mean(), and explained chunk option. Two of three components complete; minor errors in formatting, chunk option, or explanation. One component complete, or chunk label, option, or explanation missing. 5 / 3 / 1
1.3 Knit & Reflect Document knits; all three questions answered in complete sentences with specific observations, including testing toc: false. Document knits; two questions answered, or answers vague but show understanding. Document knits; one question answered or answers show minimal engagement. 5 / 3 / 1
Part 2 — Debugging Exercises (20 pts)
A Case-Sensitive Name Original chunk preserved; fix runs correctly; explanation names the error (case sensitivity) and debugging step. Code corrected; explanation partially accurate or wrong debugging step named. Partial fix or explanation absent. 5 / 3 / 1
B NA in mean() Fix uses na.rm = TRUE; explanation describes NA propagation and references ?mean Arguments section. Code corrected; explanation omits ?mean or misidentifies the mechanism. na.rm not used correctly; explanation absent or inaccurate. 5 / 3 / 1
C Date Class & max() Both lines present; explanation distinguishes lexicographic (character) vs. chronological (Date) comparison with a concrete example. Both lines run; explanation partially correct but does not distinguish the two comparison types. Only one line addressed; explanation absent or incorrect. 5 / 3 / 1
D Join Key Mismatch Corrected join uses by = c(“id” = “ID”) or renames column; explanation cites ?dplyr::left_join and describes the by argument. Join corrected; explanation omits ?dplyr::left_join or is incomplete. Partial fix; explanation absent. 5 / 3 / 1
Part 3 — Write a Reprex (10 pts)
3.1 Problem Identification Describes expected vs. actual behavior; class() output included; explains why the class prevents the operation. class() used; explanation of why the class is problematic incomplete. class() not used or explanation missing. 3 / 2 / 1
3.2 Reprex Quality All four components present; error reproduces; fix shown in second chunk with accurate explanation. Three components present; error reproduces; fix shown but explanation brief. Reprex incomplete (external data, missing packages); error may not reproduce; fix or explanation absent. 7 / 4 / 1
Part 4 — Semester Project Start (5 pts)
4 Project Start Complete YAML; Level 1 heading; labeled chunk with option loading a relevant package; 3+ sentence paragraph specific to the student’s project. YAML and heading present; chunk missing options or generic; paragraph vague. YAML incomplete; chunk unlabeled or absent; paragraph too short or off-topic. 5 / 3 / 1

Grade Scale

A (45–50) B (40–44) C (35–39) D (30–34) F (< 30)

Instructor Feedback

Points earned: ________ / 50    Date returned: _______________

Comments: