HW1: A Quarto tutorial + intro to R

Author

Your Name

Published

23 November 2025

Welcome to Your First Quarto Document!

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

  1. Read the text: Instructions appear in regular text like this
  2. Run code chunks: Gray boxes contain R code - click the green “play” button to run them
  3. See outputs: Results appear right below the code
  4. Render the document: Click “Render” button above to create the final HTML document

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

  1. Practice running code chunks in this document
  2. Try the exercises above
  3. Modify existing code (starting by adding your name and a new title in the yaml) and observe what changes
  4. Click “Preview” to create your HTML document
  5. 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
  • Render document: Click “Preview” button (or Ctrl/Cmd + Shift + K)
  • Insert code chunk: Ctrl/Cmd + Alt + I
  • Keyboard shortcut for <-: Alt + - (minus)

Remember: Save your work frequently (Ctrl/Cmd + S)!