Foreword
“You know, people think mathematics is complicated. Mathematics is the simple bit. It’s the stuff we can understand. It’s cats that are complicated.”
— John Horton Conway
In R, you can write a function that returns another function. You can put functions in a list, pass them to other functions, or build new ones while your program runs. If you came to R to analyse data, this might seem like an odd collection of tricks. Why would a language for statistics work this way?
The answer begins before there were computers to run it on. In the 1930s, Alonzo Church developed a mathematical system built around functions: the lambda calculus. Functions were not one tool among many. They were the material from which everything else could be made. Even true and false could be represented by functions: one chooses the first of two alternatives, the other chooses the second.
That way of thinking found its way into programming through Lisp, then Scheme. A function could be a value in its own right, something a program could create and work with, just as it worked with numbers.
Meanwhile, statisticians at Bell Labs were solving a different problem. Analysing data meant too much time writing programs and too little time exploring results. The language they developed, S, made that work interactive: type an expression, inspect the answer, decide what to try next.
R brought these traditions together. In the 1990s, Ross Ihaka and Robert Gentleman at the University of Auckland combined the statistical language of S with ideas from Scheme. The result was a language in which you could explore a dataset at the console and write a function that builds other functions, using the same small set of tools.
This book follows that connection. We will learn how to use R, but also where its rules come from and what they let us do. Features that look unrelated at first will begin to fit together. The aim is to make R a language you can reason about.