Assignment
Assign a Name to an Object

### Description

Assigns a value to a name using an operator.

### Usage

    expression <- value
    expression = value
    value -> expression
    expression <<- value

### Arguments

value | the value to be assigned to the output from expression. The value argument itself can be an expression.
expression | a language expression. If expression is a name or character string, the assignment operator in any of its forms causes value to be saved under that name. The arrow in the assignment operator always points toward the name. The expression argument can also specify a subset, an element, or an attribute of an object, or any combination of such expressions. Expressions of this form are called replacements to distinguish them from simple assignments. Examples include: x[-1] <- 0 length(mystuff) <- n z$b <- 3 attr(obj,"myattr")[[1]] <- new.value

### Details

- The left-arrow assignment operator comprises the two characters < and -.
- An equal sign = can also be used as an assignment operator. (Note that this form is discouraged.)
- The right arrow comprises the two characters - and >.

All three of these are equivalent semantically.
Assignment Locations.

- Assignments made with <- appear in the current environment where the <- call occurs.
- Assignments made with the <<- operator cause a search for an existing object to which to make the assignment. This assignment starts in the parent environment of the environment where the <<- call occurs, and then continues searching though the parent environments. If the object is not found, the assignment takes place in the global environment.

Legal Names. A legal name can be any combination of alphanumeric characters (not beginning with a number), an underscore (_), and periods; although there are a few reserved names: break, for, if, next, repeat, and while. Non-legal names are allowed if they are quoted. Names can be any length. To avoid confusion, you should create legal names that are not built-in function names. (The most common function names users choose accidentally are c and t.)
The names of operators, including user-written operators, are formulated as "% operatorname %". See the examples below for an illustration of a user-defined operator.
Occasionally, you might try something like the following:

paste("xxx", i, sep="") <- tmp.

However, this form does not work. Instead, use the following form:

assign(paste("xxx", i, sep=""), tmp).

The global environment contains an object called .Last.value, which contains the last object that was not assigned. To keep the results of a command you just executed, you can use .Last.value.
Value
the value of the assignment is the value argument. This is also true for replacements: the value of a replacement is the substituted values, not the whole object in which the replacement occurs.
Side Effects
The expression object is given the value of the value object.
Warnings
In addition to object assignments, the equals sign = is used for argument assignments within a function definition. Because of this, there are some ambiguities you must be aware of when using the equals sign as an assignment operator. For example, the command print(x <- myfunc(y)) assigns the value from myfunc(y) to the object x, and then prints x. Conversely, the command print(x = myfunc(y)) simply prints the value of myfunc(y) and does not perform an assignment. This is because the print function has an argument named x, and an argument assignment takes precedence over object assignment with the equals sign.
Because of these ambiguities, the use of the equals sign for left assignment is discouraged.
See Also
assign, remove, rm, get, exists, attach, detach, search
Examples

    # 100 uniforms saved as x.
    # The square root of the sample saved as y
    y <- sqrt(x<-runif(100))
    # y is local to the function, but foo.foo is assigned to
    # the global enviroment.
    "fooxy" <- function(x)
    {
        y <- x + 3
        foo.foo <<- c(x, y)
        y
    }
    # Create an operator named "gauss".
    "%gauss%" <- function(x, y) exp(-x^2/y)
    # Save results using .Last.value.
    my.x <- runif(20)
    my.y <- rnorm(20, 4*my.x, .5)
    lsfit(my.x, my.y)
    my.xyreg <- .Last.value

Package base version 6.1.5-18
Package Index