assign
Assign Object to Environment

Description

Assigns a value to a name. The location of the assignment can be a specific environment (which is useful when writing functions) or the current environment.

Usage

assign(x, value, pos = -1, envir = as.environment(pos), 
   inherits = FALSE)

Arguments

x a character string giving the name of the object to be assigned.
value any object. The value to be assigned the name in x.
pos an integer, a character string, or an environment to specify in which environment to assign the object. If you specify a string, it is used to find a package on the search list, and the assignment uses the environment for that package. The default assigns to the current environment. See as.environment for more details.
envir the environment to assign to.
inherits a logical value. If FALSE (the default), the assignment is made in the specified environment. If TRUE, first the environment specified by pos and/or envir is searched, and then its parent environments are searched until the name specified by x is found, and the assignment is made in that environment. If the name is not not found, the assignment is made in the global environment.
Value
returns value.
Side Effects
The x argument provides the name of the object value that is created.
If you call assign("a\$b", 1), you create an object named a\$b. Then you can call the get function to access such an object. Using this technique, you can create objects (to correspond with objects in another language, perhaps) whose names are not legal object names.
Note that the above example does not assign the b component of object a. If you want to assign the b component of the object a, create a new object with component b and then assign the entire object to a.
See Also
remove, get, exists, as.environment, search
Examples
assign("abc", 1:3) # assign "abc" 
# make up variable names in a loop 
for(i in 1:10) assign(paste("sample", i, sep="."), runif(10)) 
# assign to a specific environment
e <- new.env()
assign('x', 42, envir=e)
Package base version 4.0.0-28
Package Index