2 Getting started

This chapter will give you the minimum information you need to know in order to get started reading this book, and it will point you to other places in the book where you can find more information.

2.1 Installing R and RStudio

R is freely available from CRAN (the Comprehensive R Archive Network). How you install R will depend on your operating system. See the sections on installing R on a Mac (A.2), Windows (A.4), or Ubuntu (A.3) for details.

You should also install RStudio Desktop, an integrated development environment for R. While RStudio is not strictly necessary, you will find it enormously helpful for writing R code and understanding what is happening in your session.

2.2 Packages you will need

Most of this book uses the tidyverse of R packages. These packages are designed to work together using a concept called tidy data. For an explanation of this concept, see Hadley Wickham’s “The Tidy Tools Manifesto,” which is part of the tidyverse package.11 We can install most of the packages that we need by installing the tidyverse package. For now, you can run the following code in your R session. See the section on installing packages from CRAN for a fuller explanation (section A.6).


Most of the examples in this book are available in the historydata package.12 While this book is being written, you will need the development version of the package, which is available on GitHub. You can install it directly from GitHub using the devtools package by running the code below. See the section on installing packages from GitHub for a fuller explanation (section A.7).


You can test that you have these packages installed by loading them in your R session. Note that tidyverse prints a message about the other packages that it is loading, but this is not an error message. See the section on packages and libraries for more information about loading packages (section A.5).

#> Loading tidyverse: ggplot2
#> Loading tidyverse: tibble
#> Loading tidyverse: tidyr
#> Loading tidyverse: readr
#> Loading tidyverse: purrr
#> Loading tidyverse: dplyr
#> Conflicts with tidy packages ----------------------------------------------
#> filter(): dplyr, stats
#> lag():    dplyr, stats

You will need other packages for different chapters in this book. You can install them from CRAN as you go. See the package guide for a description of other packages that may be helpful to you (appendix B).

2.3 Learning R

This book is not intended to teach you how to program in R. If you want a general purpose introduction to the language, I recommend the following sequence of books.

  • Garret Grolemund’s Hands-On Programming with R is a good introduction to the basics of R programming.13
  • Hadley Wickham and Garret Grolemund’s R for Data Science teaches some of the basics of R programming in the process of teaching data analysis. If you have already programmed before, this is the place to start.14
  • Hadley Wickham’s Advanced R is the definitive guide to the language. Once you have a journeyman’s proficiency in R, use this book to gain a deeper knowledge.15
  • Norman Matloff’s The Art of R Programming is another excellent, in-depth guide to the language as a whole.16

This book does offer an R primer (appendix C). This will explain the very basics of R in a condensed form. This should be enough to get you started, especially if you are already familiar with programming concepts from a different language.

  1. Hadley Wickham, Tidyverse: Easily Install and Load ’Tidyverse’ Packages, 2017, https://CRAN.R-project.org/package=tidyverse. For a more rigorous explanation, see Hadley Wickham, “Tidy Data,” The Journal of Statistical Software 59, no. 10 (2014), http://www.jstatsoft.org/v59/i10/.

  2. Lincoln Mullen, Historydata: Data Sets for Historians, 2016, https://github.com/ropensci/historydata.

  3. Garrett Grolemund, Hands-on Programming with R: Write Your Own Functions and Simulations (O’Reilly Media, 2014).

  4. Hadley Wickham and Garrett Grolemund, R for Data Science: Import, Tidy, Transform, Visualize, and Model Data (O’Reilly Media, 2017), http://r4ds.had.co.nz/.

  5. Hadley Wickham, Advanced R (Chapman & Hall/CRC, 2014), http://adv-r.had.co.nz/.

  6. Norman S. Matloff, The Art of R Programming: Tour of Statistical Software Design (No Starch Press, 2011).