01 / 21
<-

Variable Assignment
<- vs. = in R

Master R's two assignment operators, understand the community convention, and know exactly when each one works.

Computer Science R Programming

What Is a Variable?

A variable is a named container that stores a value in your computer's memory. When you assign a value to a variable, you are telling R: "remember this value under this name so I can use it later."

x NAME <- 42 VALUE

R Has Two Assignment Operators

<- Arrow Operator
The traditional R assignment operator. Read it as "gets" or "is assigned."
= Equals Operator
Also performs assignment at the top level, but carries a different meaning in function calls.

Why Does R Have Both?

R evolved from the S language (created at Bell Labs in 1976). S used <- because early keyboards had a dedicated left-arrow key. When R added support for =, the arrow was already deeply embedded in the culture.

1976 S Language uses <- 1993 R Created inherits <- 2001 R 1.4.0 adds = for assignment

The Golden Rule of R Style

R Convention
# ✓ Use <- for assignment
x <- 42

# ✕ Avoid = for assignment
x = 42

Where They Differ

Inside a function call, = sets a named argument, while <- creates a variable in the calling environment. This is the critical behavioral difference.

Example: Inside a function call
# Sets argument n to 5 (no side effect)
rnorm(n = 5)

# Creates variable n in your environment!
rnorm(n <- 5)
print(n)  # 5 exists now

Assignment Context Matters

TOP-LEVEL ASSIGNMENT x <- 10 ✓ Preferred x = 10 Works, but unconventional INSIDE A FUNCTION CALL f(n = 5) ✓ Sets argument f(n <- 5) ✕ Side effect!

The Simple Rule to Follow

1
Use <- for assignment. Whenever you want to store a value in a variable, use the arrow.
2
Use = only for function arguments. Inside parentheses when calling a function, use equals to set named parameters.
3
Add spaces around operators. Write x <- 5 and f(n = 3), not x<-5 or f(n=3).

Example 1: Basic Assignment

R Script
# Store values
name <- "Alice"
age  <- 17
gpa  <- 3.85

# Use the variables
paste(name, "is", age, "with GPA", gpa)
# [1] "Alice is 17 with GPA 3.85"

Example 2: Mixing Contexts

R Script
# Assignment: use <-
scores <- c(88, 92, 76, 95, 81)

# Function argument: use =
avg <- mean(scores)

# Named argument inside function
round(avg, digits = 1)
# [1] 86.4

Example 3: The Trap in Action

Dangerous Code
# What does this do?
rnorm(n <- 3)
# [1] -0.12  1.47  0.83

print(n)
# [1] 3   ← n now exists in your environment!
Safe Code
# Use = for the argument
rnorm(n = 3)
# [1]  0.55 -1.20  0.32

print(n)
# Error: object 'n' not found   ← no side effect!

Other Assignment Operators

R actually has five assignment operators, but you will almost exclusively use just two.

Operator Direction Usage
<- Left assign Standard: use this
= Left assign Arguments only
-> Right assign Rarely used
<<- Global left Advanced: avoid for now
->> Global right Advanced: avoid for now

Assignment Scope Explained

Global Environment x <- 10 y <- 20 Function Call: plot() x = 1:10 stays inside function type = "l" stays inside function

Spacing for Readability

Good Style
result <- 5 + 3
name   <- "Bob"
mean(x, na.rm = TRUE)
Poor Style
result<-5+3
name="Bob"
mean(x,na.rm=TRUE)

Common Mistakes

✕ Using = for assignment
Writing x = 5 instead of x <- 5. It works, but violates convention and confuses readers.
✕ Using <- in function calls
Writing f(n <- 5) instead of f(n = 5). This creates an unintended variable n in your environment.
✕ Missing spaces
Writing x<-3 without spaces. This can be misread as the comparison x < -3.

Summary Comparison

Feature <- =
Top-level assignment ✓ Yes ✓ Yes
Inside function args Creates side effect ✓ Sets argument
Convention ✓ Preferred Unconventional
Keyboard shortcut Alt + - Just press =
Used in style guides ✓ All major guides Not recommended

Question 1 of 5

Which operator is the conventional way to assign a value to a variable in R?

Question 2 of 5

What happens when you write rnorm(n <- 10)?

Question 3 of 5

Why is spacing important around the <- operator?

Question 4 of 5

Which line of code follows best R style?

Question 5 of 5

In RStudio, what is the keyboard shortcut to type the <- operator with proper spacing?