Getting Started with R
You are encouraged to use R for assignments in this course even if you have never used it before. R is a free programming language used extensively for data analysis, statistics, and graphics. R is similar to Stata in terms of doing econometrics and data analysis. But it is free and therefore much more widely used in the real world. For this reason its worth learning. All the solutions in this course will also be provided in R.
Most people use R through RStudio, which provides a convenient interface for writing code, running commands, viewing data, and making figures.
1. Install R
First, install R, the programming language itself.
- Go to the R Project download page.
- Download the current version of R for your operating system.
- Run the installer.
- The default installation options should be fine for most users.
2. Install RStudio
Next, install RStudio Desktop, the program you will use to work with R.
- Go to the RStudio Desktop download page.
- Download the free version for your operating system.
- Install it using the default options.
Install R first, then RStudio.
3. Make sure everything works
Open RStudio. Find the Console pane and type:
2 + 2
Press Enter. You should see:
[1] 4
If so, R is working correctly.
4. Write code in an R script
For most work, you should write your code in a script rather than typing everything directly into the Console.
- Select File → New File → R Script.
- Write commands in the script.
- Run the current line or selected lines using:
- Mac:
Command + Enter - Windows:
Ctrl + Enter
- Mac:
- Save the script as you work.
An R script is just a text file containing the commands needed to reproduce your analysis.
5. Installing packages
Much of R’s functionality comes from add-on packages. A package only needs to be installed once on your computer.
For example:
install.packages("tidyverse")
Once installed, load the package at the beginning of a new R session with:
library(tidyverse)
If R tells you that a package is missing, installing it is often the first thing to try.
6. Getting help
You are not expected to memorize R syntax. Looking up commands, reading examples, and getting help when your code produces an error are normal parts of programming.
Useful resources include:
- Getting Started with R — Posit
- Getting Started with RStudio — Posit
- Download R — The R Project
- Download RStudio Desktop — Posit
You may also use web searches, classmates, course staff, and AI coding assistants.
If you use an AI coding assistant, try to describe what you want the computer to do rather than simply asking it to complete an assignment. Ask it to explain code you do not understand, and make sure you can interpret the output it produces.
7. A good habit: make your work reproducible
Whenever possible, keep the commands needed for an analysis in a saved script. Someone should be able to open your script, run it from beginning to end, and reproduce your calculations, tables, and figures.