Week 1: Welcome & The Modern R Toolkit

GPH-GU 2182 Β· Statistical Programming in R

Yang Feng

2026-09-02

Welcome to GPH-GU 2182!

Why Statistical Programming in R?

Why R?

  • Public Health Standard: CDC, WHO, FDA, academic journals, and health agencies run on R.
  • Tidyverse Ecosystem: Unmatched tools for data wrangling (dplyr), visualization (ggplot2), and modeling.
  • Open & Reproducible: Free, community-driven, transparent science.

Why This Course?

  • No prior programming required.
  • Full data pipeline: Import \(\rightarrow\) Clean \(\rightarrow\) Visualize \(\rightarrow\) Simulate \(\rightarrow\) Report.
  • Modern professional toolkit: Quarto + Git/GitHub + Responsible AI assistance.

The Modern Data Science Stack

In this course, you will master five core technologies:

  1. R: The statistical computing engine.
  2. RStudio / Positron: The integrated development environment (IDE).
  3. Quarto (.qmd): Reproducible scientific publishing (replaces Word/PDF/slides).
  4. Git & GitHub: Version control, collaboration, and automated grading.
  5. AI Assistants: Prompting, verifying, and debugging code with LLMs.

Choosing Your IDE: RStudio vs. Positron

🟦 RStudio Desktop (Class Standard)

  • Primary demo environment used on screen during class.
  • Matches 1-to-1 with screenshots in R Programming: Zero to Pro (Ch 1).
  • Classic 4-pane layout (Source, Console, Environment, Plots).
  • Pre-installed on NYU Bobst library workstations.

🟣 Positron (Supported Alternative)

  • Posit’s next-generation IDE built on VS Code.
  • Superb native GitHub Copilot & AI sidecar integration (we will demo this in Week 8!).
  • Native R + Python multi-language switching.
  • 100% compatible: All course Quarto files and Git repos run identically in both!

How Our 160-Minute Sessions Work

We do not do passive lectures. Each class is split into interactive cycles:

%%{init: {'theme': 'base', 'themeVariables': { 'fontSize': '18px'}}}%%
flowchart LR
    A["πŸ’» Mini-Demo<br/>(15–20 min)<br/>Instructor Live Code"] --> B["πŸ‘₯ Paired Exercise<br/>(10–15 min)<br/>Hands-on Problem"]
    B --> C["πŸ’‘ Debrief<br/>(2 min)<br/>Solution & Takeaways"]
    style A fill:#e8f4f8,stroke:#2980b9,stroke-width:2px
    style B fill:#eafaf1,stroke:#27ae60,stroke-width:2px
    style C fill:#fef9e7,stroke:#f39c12,stroke-width:2px

πŸ’» Mini-Demo
(15–20 min)
Instructor Live Code

πŸ‘₯ Paired Exercise
(10–15 min)
Hands-on Problem

πŸ’‘ Debrief
(2 min)
Solution & Takeaways

  • CAs & Instructor circulate during exercise blocks to assist.
  • You push your in-class exercises to GitHub for participation credit.
  • Bi-weekly 20-minute no-AI quizzes certify your personal hand-fluency.

Cycle 1: R Fundamentals & Packages

R as an Interactive Calculator

You type commands into the Console; R evaluates them and prints results.

# Arithmetic
12 + 35
[1] 47
(100 - 32) * 5 / 9   # Fahrenheit to Celsius
[1] 37.77778

Tip

Everything after # is a comment and is ignored by R. Use comments to document your thinking!

Built-in Math Functions

sqrt(64)
[1] 8
exp(1)
[1] 2.718282
log(10)              # Natural logarithm (base e)
[1] 2.302585
log10(100)           # Common logarithm (base 10)
[1] 2

Objects and Variable Assignment

We store values in named objects using the assignment operator <- (Alt + - or Option + -):

# Assigning a value
participant_age <- 28
height_m <- 1.75
weight_kg <- 68.5

# Using objects in expressions
bmi <- weight_kg / (height_m^2)
bmi
[1] 22.36735
  • Names must start with a letter and can contain letters, numbers, _, and ..
  • R is case-sensitive (bmi \(\neq\) BMI).

Functions and Getting Help

Functions take arguments (inputs) and return an output:

# Rounding
round(bmi, digits = 1)
[1] 22.4
# Summary statistics
vals <- c(10, 20, 30, NA)
mean(vals, na.rm = TRUE)
[1] 20

Getting help in R:

  • ?round or help(round): opens the official documentation.
  • example(round): runs examples from the help page.

Packages & The Course Textbook: R02Pro

R packages extend R with datasets and specialized functions.

# Install once per computer (done in Console, not script):
install.packages("r02pro")

# Load in every session where you need it:
library(r02pro)

πŸ“– Help Improve the Textbook (Extra Credit)

The textbook is open source; you will read it every week, so you are its best proofreaders.

  • Spot a typo or code error? Click the edit button on any book page, fork, fix, and submit a pull request: +0.5% per merged PR.
  • Found something confusing you cannot fix? Open an issue: +0.25% if accepted.
  • Capped at +2% on your final grade. First report wins, so search existing PRs/issues before submitting.
  • Every contributor is named in the book’s acknowledgments, permanently.

Get started today

⭐ Star and πŸ‘€ watch github.com/r02pro/r02pro_source, then read the contributing guide. Full rules in the syllabus.

πŸ’» In-Class Exercise 1A (12 min)

Paired Hands-on Exercise

  1. Open RStudio.
  2. In the console, calculate the Body Mass Index (BMI) for a person who is \(1.82\text{ m}\) tall and weighs \(79\text{ kg}\). Round the answer to 2 decimal places.
  3. Verify that r02pro is installed: run library(r02pro) and look at the ahp dataset (head(ahp)).
  4. Look up the documentation for log() using ?log. What is the default base?

Cycle 2: Reproducible Reports with Quarto

Why Quarto?

❌ The Traditional Way

  1. Run code in R \(\rightarrow\) Copy table to Excel \(\rightarrow\) Paste into Word \(\rightarrow\) Export PDF.
  2. Data changes? Repeat everything manually and risk copy-paste errors!

βœ… The Quarto Way

One reproducible .qmd document combining:

  • Narrative text (Markdown)
  • Executable code (R chunks)
  • Dynamic tables & plots compiled directly on demand.

Anatomy of a Quarto Document (.qmd)

A .qmd document combines three elements:

1. YAML Metadata (Header):

---
title: "My Analysis"
author: "Your Name"
format: html
---

2. Markdown Narrative (Text):

## Introduction
Here is text with **bold** & *italics*.

3. Code Chunks (Live Computation):

# An R code chunk
mean_age <- mean(c(25, 30, 45))
mean_age

\(\longrightarrow\) Click Render (Ctrl/Cmd + Shift + K) to compile directly into a polished HTML report!

Markdown Text Formatting Cheat Sheet

Syntax Rendered Output
# Heading 1 Largest Section Header
## Heading 2 Sub-section Header
**bold text** bold text
*italic text* italic text
- item 1 Bulleted list
1. item 1 Numbered list
$E = mc^2$ Inline math: \(E = mc^2\)

Code Chunk Options

Control how code executes and displays using #|:

#| echo: true       # Show code in final report (default)
#| eval: true       # Run code (default)
#| warning: false   # Suppress package warnings
#| message: false   # Suppress package startup messages
#| fig-width: 7     # Control plot width

Click the Render button (or Ctrl/Cmd + Shift + K) in RStudio to generate your HTML report!

πŸ’» In-Class Exercise 1B (15 min)

Paired Hands-on Exercise

  1. In RStudio: File \(\rightarrow\) New File \(\rightarrow\) Quarto Document…
  2. Title: Week 1 In-Class, Format: HTML.
  3. Add a level-2 header ## Health Calculation.
  4. Insert an R chunk calculating the mean and standard deviation of five systolic blood pressure readings: 120, 134, 118, 142, 128.
  5. Click Render and inspect the output in your browser!

Cycle 3: Git, GitHub, and Classroom

Why Version Control?

❌ The File Chaos (Without Git)

  • report_v1.docx
  • report_v2.docx
  • report_v2_edit.docx
  • report_final_REALLY_FINAL.docx
  • 😱 Which file has the correct numbers?

βœ… The Git Way

  • Recorded history: Every change is a snapshot with a commit message.
  • Safe experiments: Try new ideas on branches without breaking main code.
  • Seamless team collaboration: No emailed zip files or overwritten work.

The Core Git Workflow in RStudio

%%{init: {'theme': 'base', 'themeVariables': { 'fontSize': '16px'}}}%%
flowchart LR
    A["πŸ“ <b>Working Copy</b><br/>Edit files & .qmd"] -->|"1. git add<br/>(Stage)"| B["πŸ“¦ <b>Staged Changes</b><br/>Check boxes in RStudio"]
    B -->|"2. git commit<br/>(Snapshot)"| C["πŸ’Ύ <b>Local Repo</b><br/>Saved history"]
    C -->|"3. git push<br/>(Upload)"| D["☁️ <b>GitHub Remote</b><br/>Autograder checks code"]
    
    style A fill:#e8f4f8,stroke:#2980b9,stroke-width:2px
    style B fill:#fef9e7,stroke:#f39c12,stroke-width:2px
    style C fill:#eafaf1,stroke:#27ae60,stroke-width:2px
    style D fill:#f4ecf7,stroke:#8e44ad,stroke-width:2px

1. git add
(Stage)
2. git commit
(Snapshot)
3. git push
(Upload)

πŸ“ Working Copy
Edit files & .qmd

πŸ“¦ Staged Changes
Check boxes in RStudio

πŸ’Ύ Local Repo
Saved history

☁️ GitHub Remote
Autograder checks code

How Homework Works (Instant Feedback)

  1. Accept: Click the accept link for weekly-exercise-01 on the Week 1 page (after joining the org and linking your NetID).
  2. Clone: In RStudio \(\rightarrow\) New Project \(\rightarrow\) Version Control \(\rightarrow\) Git \(\rightarrow\) Paste URL.
  3. Solve: Open exercise.qmd, assign answers to q1, q2, etc.
  4. Render: Confirm the HTML compiles cleanly.
  5. Push: Commit and push to GitHub.
  6. Instant Report: Go to the Actions tab on GitHub: within 2 minutes, see which questions pass.
  7. Iterate: Revise and re-push freely until the deadline!

Cycle 4: AI Policy & Next Steps

The Three AI Zones

🟒 Zone 1: HW & Project

  • AI Encouraged
  • Use verify-then-use.
  • Disclose prompts & changes in AI appendix.

πŸ”΄ Zone 2: Quizzes

  • Strictly No AI
  • 5 bi-weekly 15-min hand-fluency checks.
  • Paper or locked browser.

🟑 Zone 3: In-Class

  • AI Allowed
  • Discuss with pair first.
  • Ask CA / Instructor if stuck.

AI Corner: The β€œVerify-Then-Use” Rule

❌ The Bad AI User

  • Prompts: β€œDo my homework question 3.”
  • Copies and pastes code blindly.
  • Submits hallucinated functions or incorrect math without understanding.

βœ… The Statistical Programmer

  • Prompts: β€œHow do I compute the trimmed mean in R while handling NAs?”
  • Inspects the output against R documentation.
  • Tests edge cases.
  • Owns and understands every line.

Summary & What To Do Before Week 2

  1. Complete & Push: In-class exercise 1 .qmd to your GitHub repo.

  2. Weekly Exercise 1: Released on GitHub; due date on the course website.

  3. Read for Next Week: R02Pro Chapters 2–3 (Vectors & Subsetting).

  4. Office Hours: Posted on the course website.