Assignment expressions
Assignment may be one of the most fundamental expressions in all programming languages. What it does is assign or bind a value to a symbol so that we can refer to the value by that symbol later.
Despite the similarity, R adopts the <-
operator to perform assignment. This is a bit different from many other languages using =
although this is also allowed in R:
x <- 1 y <- c(1, 2, 3) z <- list(x, y)
We don't have to declare the symbol and its type before assigning a value to it. If a symbol does not exist in the environment, the assignment will create that symbol. If a symbol already exists, the assignment will not end up in conflict, but will rebind the new value to that symbol.
Alternative assignment operators
There are some alternate yet equivalent operators we can use. Compared to x <- f(z)
, which binds the value of f(z)
to symbol x
, we can also use ->
to perform assignment in the opposite direction:
2 -> x1
We can even chain...