Garrett Grolemund wrote this book for people who have never programmed before, as a friendly, practical introduction to the R language. Instead of presenting the language feature by feature, it teaches programming through hands-on projects: each part of the book is built around a small simulation based on a casino game, so new concepts show up right when you need them to solve a concrete problem.

Over three projects you simulate a pair of weighted dice, model and shuffle a deck of playing cards, and build a working slot machine. Along the way you learn not only how to write R code but how R itself works, including how it stores data and how it finds and evaluates objects. These skills carry over directly to everyday data science work, and appendices cover installing R and RStudio, loading and saving data, and debugging.

Topics include:

  • Using the R console, writing scripts, and creating your own functions
  • Installing packages and reading help pages
  • Working with atomic vectors, attributes, factors, lists, and data frames
  • Selecting and modifying values with R’s indexing notation
  • Understanding environments, scoping rules, and closures
  • Writing programs with if and else statements and lookup tables
  • Building S3 classes, generic functions, and methods
  • Using loops and writing fast, vectorized code

The full book is free to read online under a Creative Commons license.