Data science

Stage 1 · Lesson 7 of 7

Reshaping between long and wide

R for data: the tidyverse

Wide data has one column per month or category, which is how people like to read tables. Long data has observation and a column saying which month or category it belongs to, which is what plotting libraries and models usually want.

Converting between them is a daily step. uses melt to go long and pivot or pivot_table to go wide; tidyr uses pivot_longer and pivot_wider. Before reshaping, decide what one row should represent in the result.

Common trappivoting wide when two rows share the same row and column labels: pandas pivot raises an error, pivot_table quietly averages them, and tidyr warns and packs them into lists, so check for duplicates first.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with

  • BookFree
    R for Data Science (2e)

    Wickham, Çetinkaya-Rundel, Grolemund · Beginner

    Free online: the Whole game and Transform chapters teach the tidyverse verbs in the order you need them

Practise

  • Pick one dataset on a topic you care about and repeat every operation in this stage on it

Keep handy

  • Set up projects, the console and Quarto documents properly before you start

Mark this stage as not done?