Variable Assignment
<- vs. = in R
Master R's two assignment operators, understand the community convention, and know exactly when each one works.
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."
R Has Two Assignment Operators
<- Arrow Operator= Equals OperatorWhy 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.
The Golden Rule of R Style
# ✓ 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.
# 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
The Simple Rule to Follow
<- for assignment. Whenever you want to store a value in a variable, use the arrow.= only for function arguments. Inside parentheses when calling a function, use equals to set named parameters.x <- 5 and f(n = 3), not x<-5 or f(n=3).Example 1: Basic Assignment
# 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
# 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
# What does this do? rnorm(n <- 3) # [1] -0.12 1.47 0.83 print(n) # [1] 3 ← n now exists in your environment!
# 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
Spacing for Readability
result <- 5 + 3 name <- "Bob" mean(x, na.rm = TRUE)
result<-5+3 name="Bob" mean(x,na.rm=TRUE)
Common Mistakes
x = 5 instead of x <- 5. It works, but violates convention and confuses readers.f(n <- 5) instead of f(n = 5). This creates an unintended variable n in your environment.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?
<- is the standard, community-preferred way to assign variables in R. All major style guides recommend it.Question 2 of 5
What happens when you write rnorm(n <- 10)?
<- inside a function call both passes the value to the function and creates a variable in the calling environment as a side effect. The safe way is rnorm(n = 10).Question 3 of 5
Why is spacing important around the <- operator?
x<-3 is visually ambiguous. A human reader (or even you, weeks later) might interpret it as "is x less than negative 3?" rather than "assign 3 to x." Spaces eliminate this confusion.Question 4 of 5
Which line of code follows best R style?
<- for the variable assignment (result <- ...) and = for the named function argument (na.rm = TRUE). Option D follows both conventions correctly.Question 5 of 5
In RStudio, what is the keyboard shortcut to type the <- operator with proper spacing?
<- with spaces on both sides. This shortcut makes the arrow operator just as fast to type as =.