01 / 21

Immutability vs.
Reassignment

Explore how R handles values that never change and names that can point to new data: two ideas that shape how every R program behaves.

Computer Science R Programming

What Is a Value in R?

Every piece of data you create in R: a number like 42, a string like "hello", or a vector like c(1, 2, 3): is stored as a value in memory. Think of a value as a sealed box: once R creates it, the contents of that particular box never change.

42 numeric "hello" character c(1,2,3) vector Values in memory

What Is a Name (Variable)?

When you write x <- 42, you are not putting 42 "inside" x. You are creating a name called x and attaching it like a label to the value 42 that already exists in memory.

x NAME points to 42 VALUE sealed in memory

Why Does This Matter?

Understanding how R separates names from values helps you predict program behaviour, avoid bugs, and reason about what your code is actually doing when it "changes" a variable.

Debugging
Know whether your data actually changed or you just moved a label.
Performance
R uses "copy-on-modify" behind the scenes: understanding immutability explains when copies happen.
Correctness
Prevents accidental side effects when passing data to functions.

The Core Rule: Immutability

Step 1: x <- 10 x 10 Step 2: x <- 20 x 10 20 new value The old value 10 was never modified: x now points to a brand-new 20

The Core Rule: Reassignment

R code
x <- 10 # name x -> value 10 x <- 20 # reassign: x -> NEW value 20 print(x) # 20

Immutability vs. Reassignment

Immutability Reassignment
What it governs Values (data) Names (variables)
Rule Values cannot be changed in place Names can point to new values anytime
Analogy A sealed envelope Moving a sticky note to a new envelope
Who decides? R's internal engine You, the programmer
Visible effect Old data is never corrupted Variable appears to "change"

How to Think Through R Code

1
Identify the name on the left side of <-. That is the label being created or moved.
2
Evaluate the right side. R computes a new value: it never modifies an existing one.
3
Bind the name to that new value. If the name already existed, this is reassignment: the old binding is simply dropped.
4
Old value persists until R's garbage collector reclaims memory: it was never mutated.

Example 1: Simple Reassignment

R code
score <- 85 score <- score + 10 print(score) # [1] 95
After line 1 score 85 After line 2 score 85 95 new value

Example 2: Two Names, One Value

R code
a <- c(1, 2, 3) b <- a # b points to the SAME value b <- c(4, 5, 6) # reassign b to new value print(a) # [1] 1 2 3 — unchanged!
After b <- a a b c(1,2,3) After b <- c(4,5,6) a b c(1,2,3) c(4,5,6)

Example 3: Vectors and Indexing

R code
v <- c(10, 20, 30) v[2] <- 99 print(v) # [1] 10 99 30
Original (untouched) 10 20 30 New copy (v now points here) 10 99 30 v

R's Secret: Copy-on-Modify

R is smart about immutability. It does not copy a value the moment you create a second name for it. Instead, it waits until you try to "modify" one copy: only then does R create a new value. This is called copy-on-modify.

1
Bind: a <- c(1, 2, 3) creates one value. Both a and b <- a share it.
2
Trigger: Writing b[1] <- 99 triggers a copy because b needs to differ from a.
3
Copy: R copies the entire vector, makes the modification in the copy, and points b to it.
4
Result: a is untouched. b has the new version. Immutability is preserved.

Functions and Immutability

R code
add_one <- function(x) { x <- x + 1 return(x) } val <- 5 result <- add_one(val) print(val) # [1] 5 — unchanged! print(result) # [1] 6

The Exception: Environments

Almost everything in R is immutable. The main exception is environments: special objects that are modified in place. But for day-to-day R coding, you rarely manipulate environments directly.

Immutable
Vectors, lists, data frames, strings, numbers, matrices
Mutable
Environments (and some specialized reference classes)

Common Mistakes

✕ "I changed the value"
Saying x <- x + 1 "changes x's value." In reality, it creates a new value and reassigns x.
✕ Expecting shared mutation
Thinking b <- a then modifying b will also change a. Copy-on-modify prevents this.
✕ Confusing name with value
Believing the variable is the data. A variable is just a label pointing to data.

Summary at a Glance

Concept What Happens Example
Immutability Values never change in place c(1,2,3) stays as-is
Reassignment Name moves to new value x <- 20 after x <- 10
Copy-on-Modify R copies only when needed b[1] <- 99 triggers copy
Function safety Local copy inside functions Caller's data is untouched

Question 1 of 5

When you write x <- x + 1 in R, what actually happens?

Question 2 of 5

What does print(a) output?

Question 3 of 5

Immutability in R means:

Question 4 of 5

What does print(x) output?

Question 5 of 5

Which of the following is mutable (can be modified in place) in R?