Learn R

Language and environment for statistics, data analysis and graphics

22 lessons · 5 hours · 4 levels · covers R 4.5

R is a vectorised, dynamically typed language built for statistics and data analysis. It is used across research, biostatistics, finance and data science for modelling, visualisation and reproducible reports, backed by CRAN's 20,000+ packages and the tidyverse. Learning it gives you a direct line from raw data to models and publication-quality plots.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics8 lessons · 1 hour in total
2. PracticalUse it productively at work, with the usual idioms14 lessons · 3 hours in total
3. AdvancedThe less common features, performance and trade-offs19 lessons · 4 hours in total
4. ExpertInternals and the edge cases experts know22 lessons · 5 hours in total

The R tree

RRunning RRunning RVectors and vectorised operationsVectors andvectorised operationsIndexing and subsettingIndexing andsubsettingConditions and loopsConditions and loopsWriting functionsWriting functionsListsListsMissing values and factorsMissing values andfactorsData framesData framesReading and writing dataReading and writingdatalapply, sapply, vapply and Maplapply, sapply,vapply and MapStrings and regular expressionsStrings and regularexpressionsStatistics and linear modelsStatistics and linearmodelsThe pipe and dplyrThe pipe and dplyrPlotting with ggplot2Plotting with ggplot2
Essentials and Practical shown (14 of 22 lessons); the full list is below. Click a lesson to read it.

All 22 lessons, in order

Basics

  1. Running R · Essentials · 6 min

    Use the R console, scripts and Rscript, assign with <- and get help with ?.

  2. Vectors and vectorised operations · Essentials · 12 min

    Atomic vectors are R's basic data: build them with c() and operate on whole vectors at once.

  3. Indexing and subsetting · Essentials · 12 min

    Select elements with positive, negative, logical and name indexes using [ ].

  4. Conditions and loops · Essentials · 10 min

    if/else, for and while loops, and the vectorised ifelse().

Functions

  1. Writing functions · Essentials · 12 min

    Define functions with defaults and named arguments, and understand what they return.

  2. lapply, sapply, vapply and Map · Practical · 12 min

    Apply a function to every element of a list or vector without writing loops.

Data structures

  1. Lists · Essentials · 10 min

    Lists hold values of any type, including other lists; access them with [[ ]] and $.

  2. Missing values and factors · Essentials · 10 min

    Handle NA correctly in calculations and use factors for categorical data.

  3. Data frames · Essentials · 14 min

    Create, inspect, subset and modify data frames, R's table of columns.

Data analysis

  1. Reading and writing data · Practical · 10 min

    Import CSV and other files into data frames and write results back out.

  2. Strings and regular expressions · Practical · 12 min

    Build, format and search text with paste, sprintf, grepl, sub and gsub.

  3. Statistics and linear models · Practical · 15 min

    Summaries, tests and lm() with R's formula syntax, plus reading model output.

Tidyverse

  1. The pipe and dplyr · Practical · 15 min

    Chain transformations with |> and manipulate data frames with dplyr's verbs.

  2. Plotting with ggplot2 · Practical · 15 min

    Build plots layer by layer: data, aesthetic mappings, geoms, scales and facets.

Programming in depth

  1. Environments and scoping · Advanced · 14 min

    How R finds variables: environments, lexical scoping, closures and <<-.

  2. S3 classes and generic functions · Advanced · 14 min

    R's lightweight object system: class attributes, generic functions and method dispatch.

  3. Errors, warnings and conditions · Advanced · 12 min

    Signal problems with stop and warning, and handle them with tryCatch, withCallingHandlers and on.exit.

  4. Packages: using and writing them · Advanced · 15 min

    Manage dependencies, call functions with ::, and structure your own code as a package.

  5. Vectorisation and performance · Advanced · 14 min

    Measure with system.time and profvis, then speed up R with vectorisation and preallocation.

Internals

  1. Lazy evaluation and metaprogramming · Expert · 18 min

    Promises, quote, substitute and eval: how R delays evaluation and how dplyr reads bare column names.

  2. Copy-on-modify and memory · Expert · 15 min

    How R shares objects between names, when it copies them, and how to see and control memory use.

  3. S4 and Reference Classes · Expert · 16 min

    R's formal object systems: S4 classes with validity and multiple dispatch, and mutable Reference Classes.