R Assignment Operators

Assignment operators store a value into a variable. R offers several ways to assign values, each with a slightly different use case. Understanding all of them prevents confusion when reading other people's code.

All Assignment Operators in R

Operator   Direction    Example              Notes
──────────────────────────────────────────────────────────────────
<-         Left         x <- 10             Most common, preferred
=          Left         x = 10              Works everywhere
->         Right        10 -> x             Less common
<<-        Left         x <<- 10            Global assignment
assign()   Function     assign("x", 10)     Programmatic use

The Standard Assignment: <-

name    <- "Deepa"
score   <- 92
active  <- TRUE

The left-arrow <- is the official R style. The R community strongly prefers it over = for variable assignment. Use it in all your scripts for consistent, professional code.

The Equals Sign: =

The = sign works for variable assignment in most situations, but has one important limitation: inside function calls, = specifies a named argument rather than creating a variable.

# Variable assignment (both work here)
x <- 5
x = 5

# Inside function arguments, = specifies the argument name
mean(x = c(1, 2, 3, 4))   # x here is the argument name, not a new variable

The Right Arrow: ->

The right arrow assigns in the opposite direction — the value on the left goes into the variable on the right. It is rarely used but valid.

42 -> answer
"Kolkata" -> city

print(answer)   # 42
print(city)     # "Kolkata"

The Global Assignment: <<-

The double left arrow assigns a value to a variable in the global environment, even when used inside a function. This is an advanced concept — beginners should avoid it until studying scope and environments.

counter <- 0

increment <- function() {
  counter <<- counter + 1   # modifies the global counter
}

increment()
print(counter)   # 1

The assign() Function

Use assign() when you need to create variable names dynamically (programmatically).

# Create variables month_1, month_2, month_3 in a loop
for (i in 1:3) {
  assign(paste0("month_", i), i * 1000)
}

print(month_1)   # 1000
print(month_2)   # 2000
print(month_3)   # 3000

Reading Assignment Direction

x <- 10      value 10 goes INTO variable x
10 -> x      value 10 goes INTO variable x
─────────────────────────────────────────
Both lines do exactly the same thing

Compound Update Pattern

R does not have += or -= operators like some other languages. To update a variable, reassign it:

total <- 100
total <- total + 50    # add 50 to total
total <- total * 1.1   # increase by 10%
print(total)           # 165

Best Practice: Use <- for Variables

Situation                    Use
──────────────────────────────────────────────────────
Assigning to a variable      x <- value
Function argument names      func(arg = value)
Comparing values             x == value
Modifying global from inside
a function (advanced)        x <<- value

Sticking to <- for variable assignment and = only inside function arguments keeps your code clear, predictable, and consistent with how the majority of R code is written around the world.

Leave a Comment

Your email address will not be published. Required fields are marked *