expression <- value expression _ valuevalue -> expression
expression <<- value
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
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.
y <- sqrt( x<-runif(100) ) # 100 uniforms saved as x # square root of the sample saved under the name yfunction(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