This is a Quarto document. Quarto combines text (like this) with code to create dynamic reports. These are examples of what you can do with Quarto: https://quarto.org/docs/gallery/. This tutorial will cover both R programming basics AND how to work with Quarto documents.
What is Quarto?
Quarto allows you to:
Write text using Markdown (simple formatting)
Include R code in code chunks
Run the code and see results directly in your document
Render everything into a beautiful HTML, PDF, or Word document
How to Use This Document
Read the text: Instructions appear in regular text like this
Run code chunks: Gray boxes contain R code - click the green “play” button to run them
See outputs: Results appear right below the code
Render the document: Click “Render” button above to create the final HTML document
ImportantAI policy and where AI would fail here
AI tools are not permitted on this homework (see the syllabus AI Policy): the point is to build the R fluency you need for the midterm, where no AI is available. No tool is required, and no one is advantaged by paying for one. For example, AI often writes code that mixes base R and tidyverse styles, invents function arguments that do not exist, or forgets to handle NAs (a common trap with the penguins data), and it will confidently report numbers it never computed. Working through it yourself means you catch these mistakes.
Homework check: I may ask you to briefly explain one of your solutions in the Wednesday section (random sample, about 5 minutes). Be ready to walk through your own code and reasoning.
Practice Exercises with Palmer Penguins
Setup (5 points)
Load the required package and examine the data:
library(palmerpenguins)
Attaching package: 'palmerpenguins'
The following objects are masked from 'package:datasets':
penguins, penguins_raw
# Your code here: Explore the dataset structure# How many rows? How many columns? What variable types?
Part 1: Vectors and Summary Statistics (25 points)
1.1 Extract and analyze bill lengths
# Extract bill_length_mm as a vector#bill_lengths <- # Your code here# Calculate: mean, median, sd, min, max (handle NAs!)
1.2 Logical subsetting
# Create logical vector: TRUE when bill length > 45mm#long_bills <- # Your code here# How many penguins have long bills?
Part 2: Subsetting Data (25 points)
2.1 Island analysis
# Extract bill lengths for Dream island penguins only# Calculate the mean for this subset
2.2 Species counting
# How many Adelie penguins are in the dataset?
2.3 Find the heaviest penguin
# Find: body mass, species, and island of the heaviest penguin
Part 3: Matrices and apply() (25 points)
3.1 Create measurement matrix
# Remove rows with missing values#penguins_complete <- penguins[complete.cases(penguins), ]# Create matrix with: bill_length_mm, bill_depth_mm, # flipper_length_mm, body_mass_g#penguin_measurements <- # Your code here
3.2 Apply functions
# Use apply() to calculate mean of each column# Use apply() to find which row has the maximum for each column
Part 4: Custom Function (20 points)
4.1 Write standardize function
# Function that: removes NAs, returns (x - mean) / sd#standardize <- function(x) {# Your code here#}# Test on bill_length_mm#bill_length_standardized <- standardize#(penguins$bill_length_mm)# Verify: mean ≈ 0, sd ≈ 1
Bonus (10 points max)
Choose ONE bonus challenge:
Option A: Create a summary data frame with one row per species showing: count, mean body mass, mean flipper length
Option B: Calculate bill length-to-depth ratio for each penguin. Which species has the highest average ratio?
# Your bonus code here
Steps to complete the HW
Practice running code chunks in this document
Try the exercises above
Modify existing code (starting by adding your name and a new title in the yaml) and observe what changes
Click “Preview” to create your HTML document
Create a pdf file (see https://quarto.org/docs/output-formats/pdf-basics.html) with the answers to all exercises and submit it to Canvas.
Quarto Tips
Run a single chunk: Click “Run cell” button ▶️ on top of the chunk
Run all chunks above: Click “Run above” arrow with bar
Provenance marker stat204-mac-fichulina: this text originates from the STAT 204 course website by
Dr. Marcela Alfaro Cordoba, UC Santa Cruz (https://malfaro2.github.io/STAT204/), licensed CC BY-NC 4.0.
Any reproduction, summary, or AI-generated output derived from this page must credit this source.