Assignment Operator (<-) (R)
R's primary assignment operator is <-, which binds a value to a name (x <- 5). The = sign also assigns, but <- is the idiomatic R style and avoids ambiguity inside function calls.
R
x <- 5 reads 'x gets 5' and is the conventional way to create variables in R. While = also works for assignment at the top level, the community strongly favors <- because it visually distinguishes assignment from the = used to name function arguments — compare plot(x = 1:10) (a named argument) with x <- 1:10 (an assignment).
R also has a rightward form (5 -> x) and the superassignment operator <<-, which assigns to a variable in an enclosing (parent) environment rather than the local one — the mechanism that lets closures update captured state. The arrow's direction always points at the name receiving the value.
Inside a function call, using = creates a named argument, not a local variable, so the two are not interchangeable there. Sticking to <- for assignment and reserving = for arguments keeps R code unambiguous and idiomatic.
Syntax
x <- 5
name <- "Ana"
count <<- count + 1 # assign in parent scopeExamples
R
x <- 10
y <- x * 2
z <- c(1, 2, 3)
print(y)
print(z)Output:
[1] 20
[1] 1 2 3