Chapter 1 Installing R on Your Computer

Before you can start analyzing economic data, you need to set up your computing environment. This chapter walks you through installing R and RStudio, and verifying that everything works correctly.

1.1 Understanding R and RStudio

You’ll need to install two separate programs: R and RStudio. This can be confusing at first, so let’s clarify what each one does.

R is the programming language itself—it’s what actually runs your code and performs calculations. You could use R on its own, but the interface is quite basic.

RStudio is an application that makes working with R much more pleasant. It provides a text editor for writing code, a viewer for plots, tools for managing files, and many other conveniences. When you work with R, you’ll almost always do so through RStudio.

Important: You must install R first, then install RStudio. RStudio needs R to function.

1.2 Installing R

The installation process differs slightly depending on your operating system.

1.2.1 Windows

  1. Go to the R Project website: https://cran.r-project.org/
  2. Click “Download R for Windows”
  3. Click “base”
  4. Click “Download R-4.x.x for Windows” (the version number will vary)
  5. Run the downloaded .exe file
  6. Follow the installation wizard, accepting the default settings

1.2.2 macOS

  1. Go to the R Project website: https://cran.r-project.org/
  2. Click “Download R for macOS”
  3. Choose the appropriate version:
    • If you have an Apple Silicon Mac (M1, M2, M3, or M4 chip), download the “arm64” version
    • If you have an older Intel-based Mac, download the “x86_64” version
    • Not sure which you have? Click the Apple menu → “About This Mac” and look at the Chip or Processor field
  4. Open the downloaded .pkg file
  5. Follow the installation prompts

1.2.3 Linux

On Ubuntu or Debian-based distributions, open a terminal and run:

sudo apt update
sudo apt install r-base

On Fedora:

sudo dnf install R

For other distributions, consult your package manager or the CRAN website for specific instructions.

1.3 Installing RStudio

Once R is installed, you can install RStudio.

  1. Go to the Posit website: https://posit.co/download/rstudio-desktop/
  2. Scroll down to “All Installers” and download the version for your operating system
  3. Run the installer and follow the prompts

RStudio Desktop is free for individual use. You don’t need the paid “Pro” version for anything in this book.

1.4 Launching RStudio

After installation, open RStudio (not R directly). On Windows, search for “RStudio” in the Start menu. On macOS, find it in your Applications folder or use Spotlight search.

When RStudio opens for the first time, you’ll see a window divided into several panels. Don’t worry if it looks overwhelming—we’ll explore each part.

1.5 A Tour of RStudio

RStudio’s interface is divided into four main areas, though you may only see three when you first open it.

1.5.1 The Console (Bottom Left)

The Console is where R actually runs. You can type commands directly here and see the results immediately. When you first open RStudio, you’ll see some text about the R version you’re running, followed by a > prompt. This prompt is R waiting for you to give it instructions.

Try typing the following and pressing Enter:

1 + 1
## [1] 2

R responds with the answer. The [1] you see before the result is R’s way of numbering output—don’t worry about it for now.

1.5.2 The Source Editor (Top Left)

This panel might not be visible when you first open RStudio. It appears when you open or create a file. The Source Editor is where you’ll write longer pieces of code that you want to save. Think of the Console as scratch paper and the Source Editor as your notebook.

To open a new R script, go to File → New File → R Script (or press Ctrl+Shift+N on Windows, Cmd+Shift+N on Mac).

1.5.3 The Environment Panel (Top Right)

This panel shows you what data and objects R currently has in memory. Right now it should be empty. As you create variables and load data, they’ll appear here. This is useful for keeping track of what you’re working with.

1.5.4 The Files/Plots/Help Panel (Bottom Right)

This multipurpose panel has several tabs:

  • Files: Browse files on your computer
  • Plots: View graphs and charts you create
  • Packages: Manage R packages (we’ll cover these later)
  • Help: Access R documentation
  • Viewer: Display other types of output

1.6 Verifying Your Installation

Let’s make sure everything is working properly. In the Console, type each of these lines one at a time, pressing Enter after each:

# Check your R version
R.version.string
## [1] "R version 4.4.0 (2024-04-24)"

You should see your R version number displayed.

# Basic arithmetic
100 * 1.05^10
## [1] 162.8895

This calculates what $100 grows to after 10 years at 5% compound interest.

# Create a simple plot
plot(1:10, (1:10)^2, main = "A Test Plot", xlab = "x", ylab = "x squared")

If a plot appears in the bottom-right panel showing a curved line, your installation is working correctly.

1.7 Keeping R Updated

R releases new versions periodically. You don’t need to update immediately every time, but it’s good practice to update once or twice a year. To update:

  1. Download and install the new version of R (same process as initial installation)
  2. RStudio will automatically detect the new version

You generally don’t need to reinstall RStudio when you update R.

1.8 Exercises

  1. Open RStudio and use R to calculate what $1,000 would be worth after 20 years at 3% annual compound interest. (Hint: the formula is \(P \times (1 + r)^t\))

  2. In the Console, type Sys.Date() and press Enter. What does this function return?

  3. Explore the Help panel by typing ?mean in the Console. This opens the documentation for the mean function. Skim through it—don’t worry if you don’t understand everything yet.

  4. Create a new R script (File → New File → R Script), type # My first R script on the first line, and save it to your computer. This is how you’ll save your work in future chapters.