Statistical Methods with R

Working in RStudio

Unit A · Chapter 01 · Lecture 01b

Developed by Jeffrey M. Girard

Roadmap: Meeting R

  1. R and RStudio

  2. Console

  3. Scripts

  4. Activity

R and RStudio

Programming is a Superpower

  • Programming is how we talk to and control computers

  • It gives us power and flexibility

    • The same instruction runs on ten rows or ten million
    • We are not limited to what a menu anticipated
  • Everything in this course is done via programming

  • Programming will leave a record of what we did (i.e., code)

    • So we can reuse it, adapt it, and share it
    • And so someone else can check it

R and Other Stats Software

  • You may have met SPSS, SAS, Stata, Mplus, JASP, or jamovi
    • All of them can run the analyses in this course
  • R is free, on every computer you will ever use
    • No licence to buy, renew, or lose when you change institution
    • JASP and jamovi are free too, and both are built on R
  • R is a real programming language, not a menu with a syntax window
    • Which is why data preparation (cleaning, reshaping, merging) is easier
  • New methods arrive as packages, years before commercial tools do

The R Ecosystem

  1. Your computer is the engine of a car
    • It provides raw power for computation
  1. The R language is the controls
    • It lets you apply and direct that power
  1. RStudio is like a fancy dashboard for the car
    • It adds information and convenience
  1. An R package is like an add-on for the car
    • It adds new features and capabilities

Installing R

Windows

  1. Open a web browser
  2. Visit cloud.r-project.org
  3. Click “Download R for Windows”
  4. Click the “base” subdirectory link
  5. Click “Download R-4.X.X” (e.g., 4.6.1)
  6. Run the downloaded .exe file
  7. Select all the default options
  8. Complete the installation wizard

Mac OS

  1. Open a web browser
  2. Visit cloud.r-project.org
  3. Click “Download R for macOS”
  4. Click “R-4.X.X-arm64.pkg” (Apple silicon) or “R-4.X.X-x86_64.pkg” (older Intel Macs)
  5. Run the downloaded .pkg file
  6. Select all the default options
  7. Complete the installation wizard

Installing RStudio

Windows

  1. Open a web browser
  2. Visit posit.co/download/rstudio-desktop
  3. Scroll down to the RStudio IDE table
  4. Find the row for “Windows”
  5. Click “RStudio-2026.XX.X-XXX.exe”
  6. Run the downloaded .exe file
  7. Select all the default options
  8. Complete the installation wizard

Mac OS

  1. Open a web browser
  2. Visit posit.co/download/rstudio-desktop
  3. Scroll down to the RStudio IDE table
  4. Find the row for “macOS 13+”
  5. Click “RStudio-2026.XX.X-XXX.dmg”
  6. Run the downloaded .dmg file
  7. Drag the RStudio icon to your Applications folder (if you want)

RStudio Window

Console

R will Grant your Wishes

  • R is a well-meaning but overly literal genie

    • It has the power to grant almost any wish
    • But we must phrase our wishes carefully!
    • We will always get what we ask for…
    • …but not always what we wanted.
  • Mastering the R language means learning…

    • How to properly phrase commands
    • How to decipher error messages
    • How to view code from R’s perspective
    • How to find and fix small mistakes

Communicating with R

  • The Console is like a chat window with R
    • You send a command to R and get a response
    • Neither side of the conversation is saved
  • An R Script is like an email thread with R
    • You send many commands to R all at once
    • Only your side of the conversation is saved
  • A Quarto Document is like a scrapbook with R
    • You can combine code and formatted text
    • Both sides of the conversation are saved

Arithmetic

10 + 3
[1] 13
10 - 3
[1] 7
10 * 3
[1] 30
10 / 3
[1] 3.333333
10 ^ 2
[1] 100

The four operators you already know, plus ^ for powers. Spaces are optional.

Pitfall: Notation R Does Not Accept

10 x 3
Error: unexpected symbol in "10 x"
10 \ 3
Error: unexpected '\' in "10 \"

Multiplication is *, never x, and division is /, the slash that leans the other way. R reads \ as the start of something else entirely.

Order of Operations

10 + 3 * 2
[1] 16
(10 + 3) * 2
[1] 26

Multiplication happens before addition, exactly as in algebra. Parentheses override that, and are worth adding whenever the order is not obvious.

Decimals and Negative Numbers

10 + -30
[1] -20
1.234
[1] 1.234
1 / 3
[1] 0.3333333

Negative numbers need no special treatment, and division gives a decimal, not a fraction. R prints as many digits as it thinks you need, not all it has.

How R Prints Numbers

09.870
[1] 9.87
9876543
[1] 9876543
9,876,543
Error: unexpected ',' in "9,"

R drops leading and trailing zeros that carry no information. Thousands separators are for humans: type the digits and nothing else.

Scripts

Creating a New Script

  • A script is just a plain text file saved with a .R extension
    • Which makes it easy to share your code with other people
  • Any one of these will create one:
    • File > New File > R Script
    • Top bar: the white square with a green plus icon
    • Ctrl+Shift+N (Win) or Cmd+Shift+N (Mac)
  • A new pane appears, called the Source Editor

Entering Commands into a Script

(1.4 + 2.8 + 9.3) / 3
[1] 4.5
30.1 - 24.7
[1] 5.4

A script holds many lines at once. Think of it as drafting an email with several instructions rather than shouting one at a time.

Running One Line of Code

  • Hitting Enter or Return does not run the code; it just adds a new line
  • To run the line your cursor is on:
    • Click the Run button (top-right of the Source Editor)
    • Or press Ctrl+Enter (Win) / Cmd+Return (Mac)
  • The results appear down in the Console

Running Many Lines of Code

  • To run several lines at once:
    • Highlight them with the mouse, then click Run
    • Or highlight them and press Ctrl+Enter / Cmd+Return
  • To run the whole script at once:
    • Click the Source button (top-right)
    • Or press Ctrl+Shift+Enter / Cmd+Shift+Return

Adding Comments to Scripts

# Calculate the average score on the quiz
(1.4 + 2.8 + 9.3) / 3
[1] 4.5
100 / 9 # a comment can also end a line
[1] 11.11111

Anything after a # is ignored by R and exists only for human readers. A comment can sit on its own line or at the end of one; the code still runs.

Saving the Script File

  • Any one of these will do it:
    • File > Save
    • Source bar: the blue and white disk icon
    • Ctrl+S (Win) or Cmd+S (Mac)
  • For now, save it wherever; we will learn about Projects soon
  • You now have a .R file you can keep or transfer
    • Only the code is saved, not the results