Skip to main content

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

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, and eigen() returns eigenvalues and eigenvectors.
  • Complex numbers. Built in. Write 2 + 3i, and use Re(), Im(), Mod(), Arg(), and Conj(). Note that sqrt(-1) gives NaN but sqrt(-1 + 0i) gives 0+1i.
  • Fourier transforms. fft() computes the discrete Fourier transform of a vector.
  • Random numbers and Monte Carlo. runif() and rnorm() draw from uniform and normal distributions, sample() draws from a vector, and set.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.