Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CMPSC301 Data Science

Activity 03: Data Manipulation and Visualization

Assigned and Due

  • Assigned: Friday, 18th September 2026

  • Due and Expiration: Monday, 21st September 2026 by class time.

Note: the expiration date is the last date you can submit your work for a grade.

Parts of the this activity were facilitated by Claude.

--- --- --- --- --- --- --- --- ---

Table of Contents

Overview

Data doesn't become useful until you shape it: filter down to what matters, add new columns, group and summarize, and then look at it. In this 45-minute, in-class activity you will rotate through three short stations built around two classic, built-in R datasets - penguins (palmerpenguins) and flights (nycflights13). At each station you will copy and paste ready-made R code into a script file using RStudio, run it, and read the console output and plots.

You will practice the core dplyr verbs (filter(), select(), mutate(), arrange(), group_by()/summarize()), basic missing-value handling, joining two related tables, and building ggplot2 visualizations (scatter plots, boxplots, bar charts, histograms) to spot trends.

By the end of this activity, you will be able to take a messy real dataset, clean it up, summarize it by group, and turn it into a plot that answers a specific question.

--- --- --- --- --- --- --- --- ---

Learning Objectives

By completing this activity, you will be able to:

  1. Wrangle data with dplyr - use filter(), select(), mutate(), arrange(), and group_by()/summarize() to answer questions about a dataset
  2. Handle missing values - detect (is.na(), sum(), colSums()) and remove (na.omit()) missing values before summarizing
  3. Join related tables - combine flights and airlines on a shared column with left_join()
  4. Visualize trends with ggplot2 - build scatter plots, boxplots, bar charts, and histograms, using color and faceting to compare groups
  5. Read plots critically - describe, in writing, what a plot does and does not show

--- --- --- --- --- --- --- --- ---

Activity Goals

This activity guides you through one combined tutorial covering three data stations:

  • Tutorial_01
    • Station 1: Palmer Penguins - Wrangling Basics - filter(), mutate(), group_by()/summarize(), missing values
    • Station 2: Palmer Penguins - Visualization - scatter plots, boxplots, bar charts, faceting
    • Station 3: NYC Flights - Wrangling & Visualization - joining tables, missing delay values, delay trends by airline

Each station pairs a short R script with wrap-up questions you will answer in writing/reflection.md.

--- --- --- --- --- --- --- --- ---

Instructions

Setting Up R (Do This First!)

IMPORTANT: All three stations share the same package requirements. Set this up once before starting:

install.packages(c("tidyverse", "palmerpenguins", "nycflights13"))

OR, if you want to first check that a library exists before automatically installing it on your machine, use the following code. This saves time in the long run since you do not have to reinstall packages you already have:

# Load required libraries, install if necessary
for (pkg in c("tidyverse", "palmerpenguins", "nycflights13")) {
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg)
    library(pkg, character.only = TRUE)
  }
}

Note: No other project setup (no uv, no virtual environment) is required for R.

Working the Stations

  1. Open each station's .R file in src/ to try working on the code by following the hints in the comments. If you get stuck, then remember that all the actual code is provided in the activity guide: Tutorial_01: Data Manipulation and Visualization. Please try to write the code without this using this document.
  2. Edit each file with working code. Be sure to remove each TODO comment as you work.
  3. Run each line in RStudio (Ctrl+Enter / Cmd+Return) and read the console output and plots
  4. Discuss what trend or pattern you see with your group (if you are working with others)
  5. Answer that station's wrap-up questions in writing/reflection.md before moving to the next station
  6. Finish the General Reflection question once all three stations are complete
  7. Play: Time permitting? PLay with the analysis shiny app provided in shiny_app/. Nothing to turn in for the app, just play and visit the code it provides.

Running the Scripts

There are two options that you can use to run this AMAZING CODE. Each station's script can be run independently, using a command like (or similar to) to the following terminal window command.

source("src/station_01_penguins_wrangling/explore_penguins_wrangling.R")

The other way which might be easier is to simply open the file in RStudio and run it going line by line. Note: This way is recommended so you can pause and read each result.

Optional: The Shiny App

Once you finish all three stations, if you have extra time (or want to keep exploring at home), open shiny_app/app.R in RStudio and click Run App. This lets you:

  • Choose a dataset (penguins or flights)
  • Choose the x/y variables, a grouping variable, and a plot type (scatter, boxplot, bar chart, or histogram)
  • See a live summary table (mean, count) for your chosen grouping variable
  • Click Show Code at any time to reveal the exact dplyr/ggplot2 code that produced the plot and table you're looking at
install.packages("shiny")   # if not already installed
shiny::runApp("shiny_app")

This is optional and not required for the deliverable, but it's a great way to test "what if I grouped by island instead of species?" style questions.

--- --- --- --- --- --- --- --- ---

Deliverable

You will submit:

  • Completed R scripts with all TODO items resolved for each station:
    • src/station_01_penguins_wrangling/explore_penguins_wrangling.R
    • src/station_02_penguins_visualization/explore_penguins_visualization.R
    • src/station_03_nycflights_wrangling_viz/explore_flights.R
  • Completed writing/reflection.md with answers to all wrap-up and reflection questions

Note: You do NOT need to submit screenshots of your plots. Your instructor will verify functionality by running your code.

Submission

This is a check mark grade.

Please submit this assignment by pushing your work to your GitHub repository. Ensure that:

  • All TODO items have been replaced with working code
  • All scripts run without errors
  • The reflection document is complete
  • Your name has been added to all source files

Use meaningful commit messages that describe what you accomplished:

git add .
git commit -m "Complete Station 1: Penguins wrangling basics"
git push

--- --- --- --- --- --- --- --- ---

GatorGrade

You can check your work by running GatorGrade:

gatorgrade --config config/gatorgrade.yml

This will verify that:

  • All required files exist
  • TODO items have been removed
  • Reflection questions have been answered

--- --- --- --- --- --- --- --- ---

Seeking Assistance

If you encounter difficulties: Clear your work space using the code given below.

First, try adding code at the beginning of each program to remove all left-over variables and plots from previous runs. This will ensure that you are performing a "clean run" of your programs with each run.

# Clear environment and plots
rm(list = ls()) # clear all variables
graphics.off()  # clear all plots
cat("\014")    # clear the console

# Load required libraries, install if necessary
for (pkg in c("tidyverse", "palmerpenguins", "nycflights13")) {
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg)
    library(pkg, character.only = TRUE)
  }
}
  1. Read the error messages carefully - R and RStudio provide helpful error messages
  2. Check the tutorial document - it contains the exact code to copy and paste. However. maybe try to write the code in R first and then check your work later?
  3. Consult the tidyverse documentation - https://www.tidyverse.org/
  4. Ask during class or office hours - bring specific questions about what you've tried
  5. Work with classmates - discuss the wrangling/plotting choices together, but write your own answers

Common Issues and Solutions

When your instructor was writing this code, the following were some of the problems that he made. Nice going, huh?!

Issue: could not find function error. This one was a silly mistake.

  • Solution: Make sure you ran library(tidyverse) (and library(palmerpenguins) / library(nycflights13) for that station) at the top of the script.

Issue: object 'penguins' not found or object 'flights' not found

  • Solution: These datasets ship inside their packages. Make sure library(palmerpenguins) or library(nycflights13) ran without error before you reference penguins or flights.

Issue: Plot window doesn't show anything.

  • Solution: Run the ggplot(...) code as one full block (don't run just the first line), and check the Plots pane in RStudio. Oops!

Issue: left_join() produces NA in the name column

  • Solution: Double check the join key spelling (carrier in both flights and airlines) and that you didn't misspell the column name. I never been good at speling!

--- --- --- --- --- --- --- --- ---

Learning Extensions (Optional)

Looking for an extra challenge?! After completing the required stations, challenge yourself with the following nuggets of fun!

  1. Try a new grouping variable - in the Shiny app, group penguins by island or sex instead of species and see if the trend changes
  2. Investigate a specific carrier or month - filter flights to one airline or one month and compare its delay pattern to the overall average
  3. Add a new summary statistic - extend a summarize() call with sd() or median() and discuss how it changes your interpretation
  4. Combine what you've learned - propose one new question about penguins or flights and write the dplyr/ggplot2 pipeline you would use to answer it (you don't have to run it)

--- --- --- --- --- --- --- --- ---

Remember: The goal is not just to run the R code, but to build the habit of turning a question into a dplyr pipeline and a plot - and to read that plot critically before drawing a conclusion. But you already know all this, right!?

Now, go get 'em!

About

Fun with writing data manipulating code. Neat-O!

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages