assign
Assign Environment Variable

Description

Assigns a value to a variable name in a specific environment or the current environment.

Usage

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

Arguments

x a character string giving the variable name to be assigned.
value any object. The value to be assigned to the variable 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 found, the assignment is made in the global environment.
immediate a logical value. This argument is ignored.
Value
returns value.
Side Effects
The object value is assigned to the variable named x in the specified environment (or possibly a parent environment or the global environment if inherits is TRUE).
If you call assign("a$b", 1), you set the variable named a$b, rather than assigning the b component of object a. Then you can call the get function to access such a variable. Using this technique, you can assign variables whose names cannot be parsed as variable names.
If the specified environment is either the base package environment (baseenv) or the base namespace environment (getNamespace("base")), the value is also assigned to the other environment.
See Also
remove, get, exists, as.environment, search, baseenv
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 6.1.1-7
Package Index