tidyr reshapes data between wide and long forms—pivot_longer and pivot_wider replace older gather/spread. Install locally; playground demonstrates base reshape() concepts.
Why reshape?
Charts and models often need one observation per row. SQL pivots in warehouses mirror this—see SQL GROUP BY and CASE patterns.
tidyr locally
library(tidyr)
# pivot_longer(cols = ..., names_to = "metric", values_to = "value")
Base mindset
wide <- data.frame(id = 1:2, a = c(10, 20), b = c(30, 40))
print(wide)
Understand wide vs long before reaching for tidyr helpers.
Important interview questions and answers
- Q: Wide vs long data?
A: Wide: many metric columns; long: key-value pairs stacked—ggplot2 often prefers long. - Q: tidyr vs SQL pivot?
A: Both reshape tables; SQL pivots at query time; tidyr in R after import.
Self-check
- Why might you pivot longer before plotting?
- What package installs tidyr?
Tip: Long format often follows SQL unpivot patterns—see SQL CASE/UNION strategies too.
Interview prep
- Why pivot long?
Many charts and models expect one row per observation-metric pair.