Welcome to Module 1

Ah, R. Some people see it and panic slightly, like a squirrel in a room full of rocking chairs. But fear not! Here, we take things one small step at a time.

By the end of this module, you will be able to:

  • Open R (or Posit Cloud) and run code
  • Understand what a variable is
  • Do basic arithmetic and store the results

Your R environment

Option 1: Posit Cloud

Run R in the cloud. I don’t recommend this long-term but it’s a decent option to get you up-and-running quickly.

  1. Go to https://posit.cloud
  2. Create a free account
  3. Start a new R project
  4. Copy the code from this course and watch the magic happen

Option 2: R on your own computer

  1. Install R
  2. Install RStudio
  3. Open RStudio
  4. Copy the code and enjoy the results

RStudio is your Graphical User Interface (GUI) to R. Think of it like this: R is the engine, RStudio is the - not engine bits of the car. The bits that allow you to actually use the engine to do something useful. I don’t know, I don’t get cars.

RStudio has four main panes:

  1. Source (top-left) – where you write scripts
  2. Console (bottom-left) – where R executes code
  3. Environment / History (top-right) – shows variables and what you’ve done
  4. Files / Plots / Packages (bottom-right) – your tools, graphs, and data

It’s important to understand that you can only save code written in your Source pane.


Opening a script

  1. Open RStudio
  2. Go to File → New File → R Script
  3. A new blank tab appears in the Source pane
  4. Type your code here

Scripts are like notebooks — you can save them, edit them, and re-run lines anytime.


Running code

  • Place your cursor anywhere on the line you want to run
  • Press Ctrl + Enter (Windows/Linux) or Cmd + Enter (Mac)
  • R will execute that line and show the result in the Console pane

Tip: You can select multiple lines and run them all at once.


First tiny spells (arithmetic)

In R, you can do simple math, like this:

2 + 3
5 * 10
10 / 2

Hit CTRL+Enter after each line. Observe what R tells you. R always tells you the answer; but does it remember it?


Variables: little boxes for numbers

Variables let you store a value for later. Imagine tiny boxes labeled with names like x or y and your values are contained in those boxes.

Run this code and see what happens. Rmember to look in your Global Environment, which is where variables are stored.

x <- 10
y <- 5
z <- x + y
z
## [1] 15

Note the <- symbol. This is the assignment arrow. It allows you to store the thing on the right as the thing on the left.

Explanation:

  • x <- 10 puts the value 10 into box x
  • y <- 5 puts the value 5 into box y
  • z <- x + y adds them and stores in box z
  • Typing z shows the result

Exercise: Change the numbers in x and y. What does z become?


Lessons learned

  • R executes code line by line
  • <- is the assignment arrow, putting values in boxes
  • Variables can be used in calculations
  • Experiment — that’s the fastest way to learn

← Previous

Next →