Data wrangling (I)

Lecture 7

Dr. Benjamin Soltoff

Cornell University
INFO 3312/5312 - Spring 2025

February 11, 2025

Announcements

Announcements

  • Meet with your project 01 teams
  • Draft proposal rendered + committed + pushed to GitHub by 11:59pm on Thursday

Visualization critique

Tortured Visualizations Department

  • What is the story?

Data wrangling with {dplyr}

Key functions from {dplyr}

  • filter()
  • arrange()
  • select()
  • rename()
  • mutate()
  • group_by()
  • summarize()
  • slice(), slice_head(), slice_tail(), slice_sample()

Data tidying with {tidyr}

Stylized text providing an overview of Tidy Data. The top reads "Tidy data is a standard way of mapping the meaning of a dataset to its structure. - Hadley Wickham." On the left reads "In tidy data: each variable forms a column; each observation forms a row; each cell is a single measurement." There is an example table on the lower right with columns "id", "name" and "color" with observations for different cats, illustrating tidy data structure.

There are two sets of anthropomorphized data tables. The top group of three tables are all rectangular and smiling, with a shared speech bubble reading "our columns are variables and our rows are observations!". Text to the left of that group reads "The standard structure of tidy data means that "tidy datasets are all alike…" The lower group of four tables are all different shapes, look ragged and concerned, and have different speech bubbles reading (from left to right) "my column are values and my rows are variables", "I have variables in columns AND in rows", "I have multiple variables in a single column", and "I don"t even KNOW what my deal is." Next to the frazzled data tables is text "...but every messy dataset is messy in its own way. -Hadley Wickham."

On the left is a happy cute fuzzy monster holding a rectangular data frame with a tool that fits the data frame shape. On the workbench behind the monster are other data frames of similar rectangular shape, and neatly arranged tools that also look like they would fit those data frames. The workbench looks uncluttered and tidy. The text above the tidy workbench reads "When working with tidy data, we can use the same tools in similar ways for different datasets…" On the right is a cute monster looking very frustrated, using duct tape and other tools to haphazardly tie data tables together, each in a different way. The monster is in front of a messy, cluttered workbench. The text above the frustrated monster reads "...but working with untidy data often means reinventing the wheel with one-time approaches that are hard to iterate or reuse."

Digital illustration of a cute fuzzy monster holding a brief case that says "tidy data," standing beside a friendly looking data table character, being welcomed with cheers by many other data tables and another cute monster jumping with joy.

Digital illustration of two cute fuzzy monsters sitting on a park bench with a smiling data table between them, all eating ice cream together. In text above the illustration are the hand drawn words "make friends with tidy data."

Common tidying tasks

Pivoting

  • pivot_longer()
  • pivot_wider()

Rectangling

  • unnest_longer()
  • unnest_wider()

Character vectors

  • separate_longer_*()
  • separate_wider_*()
  • unite()

Application exercise

Improve a sad plot

ae-06

Instructions

  • Go to the course GitHub org and find your ae-06 (repo name will be suffixed with your GitHub name).
  • Clone the repo in RStudio, run renv::restore() to install the required packages, open the Quarto document in the repo, and follow along and complete the exercises.
  • Render, commit, and push your edits by the AE deadline – end of the day

Wrap up

Recap

  • Data is often messy and needs to be transformed and reshaped for effective communication
  • {dplyr} contains functions for transforming data
  • {tidyr} contains functions for reshaping data
  • Design choices are crucial to effective storytelling with data
  • There is not inherently one “right” choice, but some choices are more effective than others

Streaming recommendation