Data Wrangling

Topic 02 · Tidy Data

Ashwin Kishtagari, MBBS, MSCI

Division of Hematology/Oncology, Vanderbilt University Medical Center

2026-08-18

Tidy Data Principles

The three rules

  1. Each variable is a column
  2. Each observation is a row
  3. Each value is a cell

Get this right and every downstream step gets easier.

The dplyr verbs (R)

library(dplyr)
mtcars |>
  filter(cyl == 6) |>
  select(mpg, hp, wt) |>
  arrange(desc(mpg)) |>
  head(3)
                mpg  hp    wt
Hornet 4 Drive 21.4 110 3.215
Mazda RX4      21.0 110 2.620
Mazda RX4 Wag  21.0 110 2.875

The pandas equivalent (Python)

import pandas as pd
mtcars = pd.DataFrame({
    "mpg": [21.0, 21.0, 21.4],
    "cyl": [6, 6, 6],
    "hp": [110, 110, 110],
})
(mtcars
 .query("cyl == 6")
 .sort_values("mpg", ascending=False)
 .head(3))
    mpg  cyl   hp
2  21.4    6  110
0  21.0    6  110
1  21.0    6  110

Grouped summaries

R

mtcars |>
  group_by(cyl) |>
  summarise(mean_mpg = mean(mpg))
# A tibble: 3 × 2
    cyl mean_mpg
  <dbl>    <dbl>
1     4     26.7
2     6     19.7
3     8     15.1

Python

(df.groupby("cyl")
   .agg(mean_mpg=("mpg", "mean"))
   .reset_index())

Summary

  • Tidy data is the foundation
  • dplyr and pandas express the same ideas
  • Pick the tool that fits the task