Intro to R + Positron

Week 1 · Tuesday, Sep 29 · Lecture

Dr. Marcela Alfaro Córdoba

Today’s plan

Time What
1:30 Setup check-in
1:40 A tour of Positron
1:55 R as a calculator
2:20 Variables and assignment
2:45 Pair exercise: write it, explain it

Learning goals: use the console and a script, do arithmetic in R, and store values with <-.

Type along. No AI: Positron Assistant and inline suggestions stay off.

Setup check-in

⏱ 5 min

On Ed Discussion (today’s check-in thread): how did this morning’s setup go?

  • 🟢 Everything works
  • 🟡 Almost: one thing is missing
  • 🔴 Stuck

🟢 people: sit next to a 🟡 or 🔴 person. We’ll finish the fixes during the exercises today.

A tour of Positron

The Positron window

  • Explorer (left sidebar): files in your open folder
  • Editor (center): your scripts. Saved, reusable work.
  • Console (bottom): where R actually runs
  • Session (right): Variables shows every object you’ve made; Plots shows figures; Help shows documentation
  • Interpreter picker (top right): which R is running

Tip

Open your stat266a folder with File → Open Folder… every time you work. Your files and your R session then start in the right place.

Console or script?

Console

  • Type a line, press Enter, see the result
  • Good for quick checks
  • Nothing is saved

Script (.R file)

  • Write your code, then run it
  • Cmd/Ctrl + Enter runs the current line
  • Cmd/Ctrl + Shift + Enter runs the whole file
  • Saved, readable, repeatable

Important

Your work lives in the script. If it’s only in the console history, it didn’t happen.

R as a calculator

Arithmetic

3 + 5
#> [1] 8
12 / 7
#> [1] 1.714286
2 ^ 4
#> [1] 16
17 %/% 5     # integer division
#> [1] 3
17 %% 5      # remainder
#> [1] 2

R uses the usual order: parentheses, exponents, multiplication and division, then addition and subtraction.

2 + 3 * 4
#> [1] 14
(2 + 3) * 4
#> [1] 20

Your turn: predict first

⏱ 4 min

On paper, without running anything, write down what R prints:

10 - 3^2
(10 - 3)^2
7 %% 3 + 1

Then run the three lines and check. Were you right? If not, why not?

This “predict, then run” habit is the Thursday discussion check. Start practicing it today.

Big and small numbers, and functions

5e3          # 5 times 10^3
#> [1] 5000
2 / 1e4
#> [1] 2e-04

A function takes inputs (arguments) in parentheses:

sqrt(81)
#> [1] 9
log(100)     # natural log!
#> [1] 4.60517
log10(100)
#> [1] 2
round(3.14159, 2)
#> [1] 3.14

Not sure what a function does? Type ?log in the console. (Much more on help next week.)

Comparing things

1 == 1       # "is equal to": two equals signs!
#> [1] TRUE
3 != 4       # "is not equal to"
#> [1] TRUE
2 > 5
#> [1] FALSE

A surprise:

0.1 + 0.2 == 0.3
#> [1] FALSE
all.equal(0.1 + 0.2, 0.3)
#> [1] TRUE

Computers store decimals approximately. Use all.equal() to compare decimal numbers.

Variables and assignment

Storing values with <-

x <- 1 / 40
x
#> [1] 0.025
log(x)
#> [1] -3.688879
x <- x + 1   # use the old value to make a new one
x
#> [1] 1.025
  • <- means “store the value on the right under the name on the left”
  • Assignment prints nothing. Type the name to see the value.
  • Watch x appear in the Variables pane.
  • # starts a comment. Use comments to explain why.

Your turn: trace it

⏱ 5 min

On paper: what are the values of height, weight, and y after these lines run?

height <- 172
weight <- 68
height <- height - 10
weight <- weight * 2
y <- height
height <- 3

Then run it and check the Variables pane. Did y change when height did?

Naming things

Names can contain letters, numbers, _ and ., must start with a letter, and are case-sensitive (Age ≠ age).

⏱ 3 min

Which of these are valid names? Predict first, then test.

min_height
max.height
_age
2weight
celsius2kelvin
MaxLength

Pick one style and stick to it. In this course we use snake_case: max_height, temp_celsius.

Managing your environment

ls()         # list everything you've created
rm(height)   # remove one object

Tip

A clean restart is a good test. Session → Restart R, then run your whole script from the top. If it still works, your script, not your memory, holds your analysis.

Packages

R’s superpowers come from packages, which add functions written by others.

install.packages("tidyverse")   # once per computer (done in the DS!)
library(tidyverse)              # every session where you use it

Note

Think of it like a phone app: you install it once, and you open it every time you want to use it.

Errors are information

log("ten")
#> Error in log("ten") : non-numeric argument to mathematical function
  • An error isn’t a verdict on you: R is telling you what went wrong
  • Read it. Which function? What was wrong with the input?
  • Next week we build a full help routine: read the error, read the docs, try a small example, then ask a person

Pair exercise: write it, explain it

⏱ 15 min

Part 1, alone (8 min): in week01/temperatures.R, write a script that:

  1. Stores a temperature in Fahrenheit in temp_f (pick any value)
  2. Converts it to Celsius in temp_c: \((F - 32) \times 5/9\)
  3. Checks with > whether temp_c is above 20
  4. Has a comment on each line saying why

Part 2, with a partner (7 min): swap laptops. Without running your partner’s script, predict what each line prints. Then run it. Tell each other one thing that was clear and one thing that could be clearer.

Hint: with temp_f <- 77, you should get temp_c equal to 25.

Key points

  • Write your code in a script, and run it with Cmd/Ctrl + Enter
  • R does arithmetic in the usual order. Use parentheses when in doubt.
  • <- stores a value. Assignment prints nothing.
  • Use all.equal(), not ==, to compare decimals
  • install.packages() once, library() every session
  • Predict, then run. Every time.

Before you go

  • Do you have questions about the DS? Lesson 1: Introduction to R and Positron on the Intro to R site
  • Setup still not finished? Stay after class
  • Keep thinking about your project topic, reach out to those classmates who have similar interests.

Next: Thursday, Oct 1 · Help + Data Structures

Still no AI, including for help. We’ll learn to use the documentation instead.