Skip to content

Data Wrangling

Data wrangling prepares raw tables for analysis. This section changes their shape, labels, and missing values to produce the tables that models and plots need. It covers computed columns, conditions, strings, dates, missing values, groups, joins, and reshapes.

Most pages pair 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.

The SQL and DuckDB lesson provides a separate route for files that you want to query before importing their rows. Its invented assay table demonstrates Parquet filters, metadata joins, and paired summaries with independent checks of the selected measurements.

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
SQL and DuckDB Query Parquet measurements and verify metadata joins before a paired summary.
Reshaping move between long and wide layouts and back
Distinct & Duplicates count unique values and remove duplicated rows

The dplyr and Polars 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. The SQL lesson supplies a separate public input and uses Python with DuckDB in its own environment.

The shared input files and page runners require access to the private companion repository. In a checkout, run a page’s run.sh from its guide folder after you build the images it names. The runner mounts the section folder so each script can read ../fixtures/ and writes its results into that page’s outputs/ folder.

Start by stating what one row represents. In this example it is one sample and one gene, so a repeated sample identifier is expected across genes. A repeated sample and gene pair needs an explanation before you remove it.

Check duplicate keys before a join, then compare the output row count with the relationship you intended. Use missing-data mechanisms to decide how to handle absent measurements.

The original index reference documents the operation compared here.