E-Commerce R

E-Commerce Sales
Analysis with R

An end-to-end R analysis pipeline that transforms 525,461 raw transactional records into five production-ready charts and a certified monthly revenue summary, revealing performance patterns for a UK online retailer.

525,461
Raw Transactions
£8.83M
Total Revenue Analysed
5
Analyses Produced
4,312
Unique Customers

The Problem

Raw transaction data, zero usable insight

The client held two years of online retail data in a single Excel file with 525,461 rows and no cleaning, no feature engineering, and no reporting structure. Returns were mixed with real sales, missing customer IDs inflated counts, and invalid price entries distorted every aggregate.

Returns mixed with real sales

Credit note invoices starting with "C" were treated as regular sales, inflating revenue and order counts. They had to be identified and excluded before any analysis was valid.

Missing customer IDs

110,855 missing values across the dataset. Customer ID nulls prevented any customer segmentation or cohort analysis, requiring targeted filtering before aggregation.

Invalid prices and quantities

Zero-price rows (service or admin entries) and negative quantities (return lines) corrupted all revenue calculations. Explicit thresholds were required before any figure could be trusted.

No engineered features

The raw file had no revenue column, no month grouping, and no return flag. Every analysis dimension had to be built from scratch using feature engineering before a single chart could be produced.

Solution Architecture

End-to-end pipeline in R

The analysis follows a structured five-stage workflow written in a single R script. Every parameter is defined at the top so thresholds are easy to adjust. The pipeline runs reproducibly from a clean session with no manual intermediate steps.

Source
Raw Excel
online_retail_II.xlsx read from data/raw/
525,461 rows 8 columns
Import · Engineer
readxl + janitor
Column rename to snake_case, date cast, revenue and return flag engineered
R 4.5.3 tidyverse
Filter · Clean
dplyr filter()
Returns, nulls, zero-price, and negative quantities removed
407,650 rows certified
Analyse · Visualise
ggplot2
5 group_by summaries, 5 charts via shared theme_sales()
5 analyses scales
Export
outputs/
5 PNG charts at print resolution + revenue_summary.csv
ggsave write_csv

Data Engineering

6-step cleaning and analysis pipeline

Every transformation was parameterised and applied in sequence. The script runs as a single block from the top, with no manual steps, producing certified outputs every time.

Import

Load raw Excel data

525,461 rows read from the source XLSX via readxl. Column names immediately converted to snake_case using janitor::clean_names().

Engineer

Feature engineering

Four derived columns added: invoice_date cast to Date, month floored to month start, year extracted, is_return flag for "C" invoices, and revenue calculated as price times quantity.

Filter

Remove invalid records

Four filters in sequence: exclude returns, drop missing customer IDs, enforce min_price of 0.01, enforce min_quantity of 1. Result: 407,650 valid sale rows retained.

Aggregate

Five analysis summaries

Five group_by summarise pipelines built: revenue and orders by month, top 10 products, top 10 countries, monthly unique customers, and average order value per month.

Visualise

Production charts with ggplot2

Five publication-quality charts built with ggplot2, sharing a theme_sales() function from R/plot_theme.R. Axes formatted with currency and comma separators via the scales package.

Export

Outputs saved to disk

Five PNG charts exported at print resolution (10x5 or 10x6 inches). Monthly revenue summary exported as revenue_summary.csv. All outputs fully reproducible by re-running from the top.

Deliverable

Five production charts and a certified CSV

Each analysis was built as a standalone aggregation pipeline before being passed to ggplot2. The shared theme_sales() function ensures consistent typography, grid lines, and spacing across every chart.

E-Commerce Sales — Monthly Overview (2009 to 2010) Certified · R Output
£8.83M
Total Revenue
19,213
Total Orders
£460
Avg Order Value
Nov 2010
Peak Month
Monthly Revenue Trend (£)
£1.17M peak
Dec 09 Mar 10 Jun 10 Sep 10 Nov 10 ▲ Dec 10
Revenue by Country (Top 5)
● UK 84% ● IE ● NL ● DE ● FR

Results

What the client gained

  • Single certified revenue baseline One post-cleaning dataset all reporting is built from, replacing ad hoc exports with different row counts and conflicting totals.
  • Product visibility for commercial decisions The top ten revenue-driving products identified by name, enabling reorder planning, promotional focus, and margin analysis.
  • Geographic expansion intelligence Revenue concentration in the UK at 84% is now visible alongside the four nearest international markets, giving a data-backed starting point for expansion.
  • Seasonal pattern awareness The November 2010 revenue spike is documented and quantified, enabling advance planning for inventory, staffing, and marketing ahead of the pre-Christmas wholesale cycle.
  • Data quality flag surfaced The "Manual" entry appearing as the third highest-revenue product was identified and flagged for investigation, preventing a miscoded entry from influencing strategy.
Valid rows certified 407,650 After removing returns, nulls, and invalid records
Charts delivered 5 Revenue trend, products, countries, customers, order value
Pipeline steps 6 Import, engineer, filter, aggregate, visualise, export
Reproducibility 100% Single script, no manual steps, clean session to full output

Top 5 Products by Revenue

# Product Revenue
01 WHITE HANGING HEART T-LIGHT HOLDER £151,624
02 REGENCY CAKESTAND 3 TIER £143,893
03 Manual  ⚠ possible miscoded entry £98,561
04 ASSORTED COLOUR BIRD ORNAMENT £70,494
05 JUMBO BAG RED RETROSPOT £51,759

Technology

Stack used

R 4.5.3
tidyverse
ggplot2
readxl
janitor
scales
here
VS Code
Shopping Basket