Skip to content

Data Wrangling

Data wrangling is where most analysis time goes. Raw tables arrive with the wrong shape, the wrong labels and the wrong holes, and the work is turning them into the table the model or the plot expects. This section teaches that work one method at a time: computed columns, conditions, strings, dates, missing values, groups, joins and reshapes.

Every page pairs R and Python in synced tabs. The R tabs use dplyr and its tidyverse neighbours. The Python tabs use Polars. Both compute on the same shared fixture, a long RNA-seq counts table with one row per sample and gene, and where the two languages compute the same quantity they agree.

The two libraries name the same table operations differently. This table is the map.

Operation dplyr (R) Polars (Python)
keep rows filter filter
keep columns select select
sort rows arrange sort
add a column mutate with_columns
summarise by group group_by + summarise group_by + agg

A shared name is not a shared syntax. dplyr reads a column as if it were a variable in scope (filter(expr, condition == "treated")), while Polars builds an expression on an explicit column object (expr.filter(pl.col("condition") == "treated")), and combines conditions with & and | rather than commas. That pl.col(...) expression style is the real difference between the two, more than the verb names. Each page in this section shows both sides for its own operation, so the map above is never the whole story.

Page What it does
Computed Columns derive columns with formulas, including grouped CPM normalization and per-gene z-scores
Conditional Logic turn values into labels with multi-branch conditions
String Ops clean messy sample names so a join stops silently failing
Gene ID Mapping fix symbols corrupted by Excel and map between symbol, Entrez and Ensembl IDs
Dates parse dates, extract parts, and catch a processing confound
Missing Values detect, drop, fill and impute the gaps
Grouped Summaries split into groups and summarise each one
Window & Rank rank within a group, pick the top, take running totals
Joins combine tables on a key, and find what never matched
Reshaping move between long and wide layouts and back
Distinct & Duplicates count unique values and remove duplicated rows

The pages share the same counts fixture and its companions, so a column computed on one page is the one the next page summarises. The code for every page lives in the companion repository under guides/data-wrangling/, one folder per page, run in the shared r-base and python-base images. Gene ID Mapping is the one R-only page: identifier reconciliation is a Bioconductor job, and the page says so.