R you ready? Getting Started with the Programming Language R [Part 1]

R you ready? Getting Started with the Programming Language R [Part 1]

Research in economics and business studies relies heavily on specialised software for dinsata preparation, econometric analysis, simulation and visualisation. The statistical programming language R is used increasingly often for these tasks, and for good reason: it is free and Open Source. This makes it a natural choice from an Open Science perspective. This article is the first of a two-part series on R for researchers in economics and business studies. This first part outlines what R offers, highlights some key differences from Stata, a widely used but proprietary programme, and points to selected resources that can help with getting started. The second part looks at how to set up a reproducible workflow in R.

Three advantages of R

Open Code is a key building block of reproducible research: anyone who wants to check or build on a study needs access to the code behind it. Yet Open Code is only half the story. If the code can only be run with a proprietary programme, access still depends on a licence. This is why it also matters which programme the code depends on. R does not make research reproducible on its own, but compared with proprietary programmes such as Stata, SPSS or Excel, it has three advantages:

  • Accessible to everyone: R and all its packages are free. Anyone can run code written in R, whether or not their institution holds a licence, including replicators, students and researchers in countries with limited research budgets. Access also continues after leaving university.
  • Transparent: The source code of R and its packages is public. Anyone can check exactly how an estimator is computed instead of relying on the documentation, and errors can be spotted and corrected by the community.
  • Interoperable: R reads and writes many formats, including Stata, SPSS and Excel files as well as open formats such as CSV, and can be combined with other languages such as Python. This makes code and data easier to reuse, one of the principles of FAIR code.

In addition, many recent econometric methods are available as R packages, often written by the developers of the methods themselves.

What changes when moving from Stata to R?

The syntax is different, but most concepts carry over. Four differences are worth knowing from the start:

  1. Several datasets side by side: Stata works with one active dataset; holding more requires frames (available since Stata 16). In R, any number of datasets (data frames) can be loaded in parallel and are addressed by name.
  2. Packages: As in Stata, additional functions come from packages. The difference: an R package is installed once with install.packages() but must be loaded with library() in every new R session, usually by listing these commands at the top of the script. Packages are distributed via CRAN (Comprehensive R Archive Network), the central online repository for R software and packages, whose Econometrics Task View gives an overview of packages relevant for empirical economic research.
  3. Results as objects: In R, every regression result is saved under a name and can be reused, compared or exported later. In Stata, estimation results are overwritten by the next estimation unless they are saved with estimates store.
  4. Missing values: In Stata, a missing value counts as larger than any number, so if income > 100000 also selects observations with missing income. R marks missing values as NA (“not available”) and does not treat them as numbers. Calculations involving missing values therefore return NA, unless you explicitly tell R to ignore them.

For the most common Stata commands, the website stata2r shows the R equivalent side by side.

Where can I learn R?

Where can I find reusable packages and support?

Many established packages were developed by researchers in economics and business studies, for example fixest (Laurent Bergé), did (Brantly Callaway and Pedro Sant'Anna) and the forecasting packages fable and forecast from Monash University. When you use them, cite them like any other source. Questions can be asked in the Posit Community forum. rOpenSci peer-reviews packages and organises community events, and R-bloggers collects tutorials. The Big Book of R lists more than 400 books on R, most of them free.

Tip: Get into the habit of writing clear, well-commented code from the start. This helps not only others but also your future self, for example when you need to retrace your analysis months later during a revision. What to keep in mind is explained in the Open Economics Guide article on making code readable and documenting it.
Coffee Lecture: How Replication Games Make Reproducibility Tangible in Economics and Business Studies
Share this page: