Assign a Name to an Object

DESCRIPTION:
Operators that assign a value to a name.

USAGE:
expression <- value
expression _ value

value -> expression

expression <<- value


REQUIRED ARGUMENTS:
value:
an S-PLUS expression.
expression:
if expression is a name or a character string, the assignment operator, in any of its forms, causes the value of value to be saved under that name. Note that the arrow always points toward the name.

The expression can also specify a subset, element, or 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


VALUE:
the value of the assignment (when used as an expression) is the value of the right-hand side (value). This is true for replacements as well; the value of a replacement is the substituted values, not the whole object in which the replacement occurs.

SIDE EFFECTS:
expression is given the value of value.

DETAILS:
The left-arrow is made up of the two characters < and -. An underscore, _, can also be used for left-arrow. Right-arrow is the two characters - and >. All three of these are equivalent semantically.

LOCATION OF ASSIGNMENTS. Assignments with t;- appear in the frame of the function call in which they occur, or in the working directory if they occur in frame 1. Replacements taking place in frame 1 will create the corresponding object in the working directory if it was found somewhere else. For example, if there is no current object iris in the working directory but iris does exist elsewhere in the search list, then dim(iris) <- c(50,12) will create the modified iris on the working directory.

The <<- operator always does assignments and replacements on the working data directory, from any frame.

Once an assignment is made, the data can be retrieved (in the same frame) by mentioning the name used in the assignment.

COMMITMENT. Assignments made to a data directory or to frame 0 are committed when S-PLUS finishes evaluating an entire expression. If an error occurs during evaluation of an expression, no assignments executed during that expression are committed.

LEGAL NAMES. A name can be any combination of alphanumeric characters and periods (.) that begins with a letter or a period, though there are a few reserved names. They can be any length, but on a few machines they are unique to only a certain length - for example, 14 characters on a few UNIX machines and 11 characters under DOS. To avoid confusion, it is best to avoid creating names that are function names. The most common function names to pick accidentally are c and t.

The names of operators, including user-written operators, are %something%.

At times, you may find occasion to try something like: paste("xxx", i, sep="") <- tmp # don't do this however, this will not work. Use instead: assign(paste("xxx", i, sep=""), tmp)

There is an object in the working directory called .Last.value which contains the last object which was not assigned. If you realize that you would like to keep the results of a command that you just executed, you can use .Last.value to do this.


SEE ALSO:
assign , objcopy , remove , rm, get , exists , attach , detach , search , synchronize .

EXAMPLES:
y <- sqrt( x<-runif(100) )      # 100 uniforms saved as x
          # square root of the sample saved under the name y

function(x) { y <- x + 3 foo.foo <<- c(x, y) } # y is local to the function but foo.foo is assigned to the working directory # if an error occurs in the function, foo.foo will not be assigned

# to create an operator: "%gauss%" <- function(x, y) exp(-x^2/y)

# to save the previous results: lsfit(my.x, my.y) # no assignment so results are only seen my.xyreg <- .Last.value # save the results