Lumenary CS · Programming languages
R
The language of statistics and data analysis: vectors, data frames, functions and the native pipe, statistical models, plots, packages and reproducible reports.
R is a free, open-source language and environment for statistics, data analysis and graphics. It was created by Ross Ihaka and Robert Gentleman at the University of Auckland, first announced in 1993, and is modelled on the S language developed at Bell Laboratories. Today it is maintained by the R Core Team and used by statisticians, epidemiologists, biologists, economists, social scientists and data analysts all over the world, with more than twenty thousand contributed packages on CRAN and thousands more for genomics on Bioconductor. Its signature ideas are that every value is a vector, that most operations work on whole vectors at once, and that statistical models are described with formulas such as weight ~ height.
This course is for people who can already program a little: if you know what a variable, a loop and a function are (the Start level of Lumenary CS or equivalent experience), you are ready. Some familiarity with averages and graphs helps; the statistics are explained as they appear. The course teaches base R, the language as it comes with every installation, and shows how the popular tidyverse packages such as dplyr and ggplot2 express the same ideas, so that you can read and write both.
To run the code yourself, install R from CRAN (there are installers for Windows, macOS and Linux) and, if you like, an editor such as RStudio or Positron from Posit. R has no official online playground, but the open-source project webR runs R in your browser. Every program marked as checked was run by Lumenary's test suite with Rscript --vanilla and R 4.1 using only the packages that come with R, and the output shown is the output it printed; everything shown also works in the current release series, R 4.6. The lessons were drafted with AI assistance and are awaiting review by a human computer scientist.
- Chloe · The Designer
“Why is the language built this way?” - Margaret · The Profiler
“What does it print, and what does it cost?” - Gideon · The Language Lawyer
“What exactly does the language guarantee?”
Foundations · 4 lessons
Foundations
Where R came from and how to run it, then the building blocks of every analysis: vectors and vectorised arithmetic, indexing, types, missing values and factors, lists and data frames.
- 1First steps in RWhere R came from and what it is for, installing it, the console and Rscript, and your first lines of R. 50 min
- 2Vectors and vectorisationThe data structure under everything in R, arithmetic on whole vectors at once, recycling, logical vectors, and the four ways to index. 55 min
- 3Types, missing values and factorsR's atomic types and how it converts between them, the four kinds of nothing, factors for categorical data, dates, and the attributes that turn vectors into richer objects. 55 min
- 4Lists and data framesLists that hold anything, the three ways to take things out of them, and the data frame, R's table, with its indexing, filtering, sorting, merging and splitting. 60 min
Practice · 3 lessons
Practice
Writing functions, control flow, the apply family and the native pipe; reading, reshaping and summarising real data; and fitting and interpreting statistical models with t.test, lm and glm.
- 5Functions, control flow and the apply familyWriting your own functions, if, switch and loops, applying functions over lists with lapply and friends, the native pipe, closures, and handling errors. 65 min
- 6Reading, cleaning and summarising dataGetting a messy file into a data frame with the right types, cleaning and recoding it, and summarising it by group with table, aggregate, tapply and reshape, in base R and the tidyverse. 65 min
- 7Statistical models: t-tests, regression and beyondModel formulas, comparing groups with t.test, linear models with lm, factors and model comparison, logistic regression with glm, simulation, and reading the results honestly. 70 min
Professional · 2 lessons
Professional
Plots with base graphics and ggplot2, packages and CRAN, reproducible environments and reports with R Markdown and Quarto, the tidyverse, testing, and a complete analysis from raw data to report.
- 8Plots, packages and reproducible reportsBase graphics and ggplot2, installing and managing packages from CRAN, reproducible environments with renv, reports with R Markdown and Quarto, the tidyverse, and testing R code. 65 min
- 9Project: a complete analysis, from raw data to reportOne question answered end to end in base R, with checks, tested functions, a sensitivity analysis, second opinions, a figure and a generated report, followed by where to go next. 70 min
Further reading and resources
- The R Project for Statistical Computing · The R FoundationThe official website: what R is, news, the R Journal, conferences and how to contribute.
- CRAN, the Comprehensive R Archive NetworkDownload R for every platform, and browse the contributed packages and task views.
- The R manuals · R Core TeamThe official manuals: An Introduction to R, The R Language Definition, R Data Import/Export, Writing R Extensions and more.
- An Introduction to R · R Core Team
- The R Language Definition · R Core TeamThe closest thing R has to a specification.
- webRAn open-source project, separate from R itself, that compiles R to WebAssembly so that it runs in a browser; useful for trying code without installing anything.