Skip to main content

R for Physics

A Whirlwind Tour of R for Physics Students

Author

Dave Rosenman

Preface

This is the R a physics student needs, and not much more. It is meant to be read in an afternoon or two, with RStudio open next to it. By the end you will be able to load a table of lab measurements, plot it with error bars, fit a model and report the slope with its uncertainty, simulate a system you cannot solve on paper, and turn the whole thing into a lab report.

R suits physics coursework for a few practical reasons. Arithmetic works on whole columns of numbers at once, so a formula in your code looks like the formula in your notebook. The data table is a built-in type, which is what a lab measurement set already is. The plotting package, ggplot2, is among the best in any language. A fit hands back standard errors along with the best values. And a report can be written so that its numbers and figures are regenerated from the raw data every time you build it.

Python and MATLAB do these jobs well too, and if your department teaches one of them, learn it. Most of what you learn here carries over.

The format is borrowed, with gratitude, from Jake VanderPlas’s A Whirlwind Tour of Python: short chapters, one idea each, lots of small examples.

NoteFree to read, free to reuse

This book is free. The text is licensed under CC BY-NC-ND 4.0. The code and the data files are MIT-licensed, so use them in your own work however you like. The source is at https://github.com/DRosenman/r-for-physics.

Who this book is for

Physics undergraduates who have never used R. You do not need to have programmed before. You do need first-year mechanics and calculus, and it helps to have written a lab report by hand or in a spreadsheet, because then you already know the problem this book solves.

If you program in another language, you can read it faster. Skim the early chapters and slow down at 6  Meet the Tidyverse.

What is in this book

Getting Started installs R and RStudio (1  Installing R and RStudio) and uses R as a calculator for constants, units, and formulas (2  R as a Calculator).

Base R covers the language itself: vectors, which hold a set of measurements (3  Vectors), writing your own functions, decisions, and loops (4  Functions, Decisions, and Loops), and data frames, R’s tables (5  Data Frames).

The Tidyverse is the set of packages most R users reach for first. 6  Meet the Tidyverse introduces it, 7  Transforming Data with dplyr transforms and summarizes tables, 8  Plotting with ggplot2 plots them, and 9  Fitting Models to Lab Data fits straight lines and curves to lab data and reads off the uncertainties.

Doing Physics puts it to work. 10  Simulating Motion steps equations of motion forward in time, 11  Orbits with orbitr simulates orbits with my orbitr package, 12  Lab Reports with Quarto writes a lab report in Quarto, and 13  Where to Go Next says what to read after this.

What you need

1  Installing R and RStudio walks through installing the software and downloading the files.

How the code works

Code appears in shaded blocks, with its output directly below. Every number and every figure in this book was produced by the code next to it when the book was built, so if you run the same code you should get the same result.

Type the code rather than copying it. You will make typos, and reading the error messages they produce is part of learning the language. Then change something, a length, a mass, a time step, and see what happens.

Where this book stops

This is a tour, and it leaves a great deal out. For the rest of the tidyverse, read R for Data Science (https://r4ds.hadley.nz), which is free online. For the physics and numerical methods behind orbit simulation, my longer book Orbital Mechanics with R (https://book.orbit-r.com) picks up where 11  Orbits with orbitr ends. 13  Where to Go Next has the fuller list.