13 Where to Go Next
This book covered a narrow slice of R, chosen so that you could analyze a lab and run a simulation as soon as possible. Here is where to find the rest. Everything listed is free to read online unless noted.
13.1 Books and guides
R for Data Science, 2nd edition, by Wickham, Çetinkaya-Rundel, and Grolemund. The obvious next book. It covers everything in Chapter 6 through Chapter 8 at full length, plus importing messy data, strings, and dates.
ggplot2: Elegant Graphics for Data Analysis. Read it when you need a figure that Chapter 8 did not show you how to make. It covers scales, facets, themes, and annotations in detail.
Advanced R, by Hadley Wickham. How the language really works: vectors, functions, environments, and why R behaves the way it does. Not a second book, but a good third one.
The Quarto guide. The reference for everything in Chapter 12, and for what that chapter left out: citations, slides, and the details of PDF output.
Orbital Mechanics with R and the orbitr documentation. More computational physics in R. The book goes deeper into the integrators from Chapter 10 and uses the package from Chapter 11 on binary stars, comets, and the solar system.
A Whirlwind Tour of Python, by Jake VanderPlas. Most physicists end up using both languages, and this is the fastest route into the other one. This book borrowed its format.
13.2 Things this book skipped
R can do all of the following, and sooner or later a physics course will ask you for one of them. Each line gives the name to search for or to type after a ?.
- Numerical integration.
integrate()evaluates a definite integral of a function of one variable. - Root finding.
uniroot()finds where a function crosses zero inside an interval.polyroot()does polynomials. - Optimization.
optimize()minimizes a function of one variable,optim()a function of several. - Differential equations. The deSolve package and its
ode()function, for when the hand-written loops of Chapter 10 are not accurate enough. - Matrices and linear algebra.
matrix()builds one,%*%multiplies,t()transposes,solve()solves a linear system or inverts, andeigen()returns eigenvalues and eigenvectors. - Complex numbers. Built in. Write
2 + 3i, and useRe(),Im(),Mod(),Arg(), andConj(). Note thatsqrt(-1)givesNaNbutsqrt(-1 + 0i)gives0+1i. - Fourier transforms.
fft()computes the discrete Fourier transform of a vector. - Random numbers and Monte Carlo.
runif()andrnorm()draw from uniform and normal distributions,sample()draws from a vector, andset.seed()makes the draws repeatable. - Units and uncertainties. The units package attaches units to numbers and refuses to add meters to seconds. The errors package attaches uncertainties and propagates them through arithmetic. The quantities package combines the two.
13.3 Keeping going
The way to learn R is to use it for something you have to do anyway, so do your next lab in it, even if a spreadsheet would be quicker the first time. You will get stuck. When you do, read the error message from its first line, then open the help page with ? and the function name and run the examples at the bottom. If that fails, cut your code down to the smallest piece that still shows the problem. Half the time you will find the mistake while cutting, and the other half you will have something short enough to show a lab partner or an instructor.