Chapter 2 Welcome to R

R is a free, open-source program used by ecologists and other scientists to work with data—from organizing spreadsheets to making figures and running analyses.

In this course, we’ll use R to learn the basics of data analysis. You do not need prior coding experience. You also do not need to understand everything right away. The goal today is simply to get R working on your computer and take the first steps.

If something feels confusing at first—that’s normal. Everyone learns R by using it, not by memorizing it.

2.1 What you need (two things)

You need two programs:

  1. R (does the calculations)
  2. RStudio (helps you write and organize code)

Think of R as the engine, and RStudio as the dashboard.

2.1.1 Step 1: Install R:

  1. Go to: https://www.r-project.org
  2. Click Download R from CRAN
  3. Choose any mirror (it truly does not matter)
  4. Download the version for your operating system (Mac, Windows, or Linux)
  5. Install using the default options

You only need to do this once per computer

2.1.2 Step 2: Install RStudio:

  1. Go to: https://posit.co/download/rstudio-desktop/
  2. Download RStudio Desktop (Free)
  3. Install using the default options

RStudio will automatically find R once both are installed

2.1.3 Step 3: Open RStudio

When you open RStudio, you’ll see four panes. You do not need to customize anything right now.

Here’s what matters most:

  • Console is where R runs code
  • Source is where you write and save code
  • Environment shows what data you have loaded
  • Plots/Files/Help is where figures and help appear

Your layout may look slightly different than your neighbor’s—and that’s OK.

2.1.4 Step 4: Download the files for this lab

2.1.4.1 Create a folder on your desktop

Create a folder on your computer called: EcologyLab

2.1.4.2 Create a CSV file

For this class, we’ll use one folder for our statistical analyses. Create a folder on your desktop labeled ‘EcologyLab’.

Now let’s collect some data!

Ask your group members the following questions:

  1. How far from home are you? (Use Google Maps to get the mileage)
  2. Do you prefer cats or dogs?
  3. How many roommates do you have?

If the weather is nice, follow your TA outside to collect data on the diameter of aspen trees.

Add your responses/tree data to this google sheet. Open the class data sheet (add your data here)

Once every has entered your responses, have your TA check-out the google sheet. When you get the greenlight, download the google sheet as a .csv file and save in your EcologyLab folder

Download the data file (.csv)

Then, download your R script: Download the R file Download and use the R script to practice with R.

You should have these two files and place them inside that folder:

  • Practice R script
  • Practice CSV data file

2.1.5 Step 5: Open the R script

In RStudio:

File → Open File → select the R script

You’ll notice:

  • Lines starting with # these are notes, not code
  • Code for installing/loading packages
  • Code for setting a working directory
  • Code for importing data
  • Code for visualizing your data

We’ll go through these together.

2.1.6 Packages (what they are & why we use them)

R comes with built-in tools, but most ecological work uses packages—collections of tools made by other scientists.

For this course, we’ll start with one package:

Install the tidyverse (one time only)

Run this once in the Console:

install.packages("tidyverse")
## 
## The downloaded binary packages are in
##  /var/folders/2n/rvc2p1f96kldhz2pzhpfzkmm_q3dh_/T//Rtmp0aDi7X/downloaded_packages

If asked, choose Yes to install dependencies.

Load the tidyverse (every session)

Every time you open R, run:

library(tidyverse)
## ── Attaching core tidyverse packages ────────────────────────────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.2
## ✔ ggplot2   3.5.2     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ──────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

If you forget to load a package, your code won’t work—and that’s one of the most common R mistakes.

2.1.7 Working directories (the simple version)

A working directory tells R where your files live.

In RStudio:

  • Go to the Files pane
  • Navigate to your EcologyLab folder
  • Click More, then Set As Working Directory

2.1.8 Importing data

Once your working directory is set, importing a CSV is simple. You can use the code in the R file, or you can import the file manually using RStudio’s menus.

Importing a CSV file manually in RStudio:

  1. In RStudio, go to the Environment pane.
  2. Click Import Dataset.
  3. Choose From Text (readr).
  4. Navigate to the folder where your CSV file is saved and select the file.
  5. Click Import.

RStudio will:

  • Load the data into your session
  • Show you the data in a spreadsheet-like view
  • Automatically generate the R code used to import the file

Tip: Manual import is helpful when you are first learning R or troubleshooting file paths. For reproducible projects, we will usually rely on code-based imports.

If the file is in your EcologyLab folder, this will work.

Once you have imported your data, run your data visualization code.

2.1.9 Download each figure

To save a figure you created in R, use the Plots tab in RStudio:

  1. Click on the Plots tab (usually in the lower-right pane).
  2. Make sure the figure you want is visible.
  3. Click Export.
  4. Choose a file format (e.g., PNG or PDF).
  5. Select a file name and save location (your EcologyLab folder is a good choice).

Saving figures this way allows you to control the file type and size, which is useful when inserting figures into reports, presentations, or lab assignments.

Put these files into a word document (this will be what you turn in)

2.1.10 Other information about this R code

2.1.10.1 Commenting your code (very important)

Anything after a # is ignored by R.

# This is a note to myself

Use comments to explain:

  • what the code does
  • why you wrote it
  • anything future-you will forget

Good code is readable code.

2.1.10.2 Getting help when things break

Everyone—including professional scientists—gets errors in R.

When you’re stuck:

1.  Copy the error message
2.  Paste it into ChatGPT
3.  Ask: “What does this error mean?”

Learning to debug is part of learning R.

2.2 Assignment

To receive credit, submit your word document through your canvas portal.