[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  assign: Assign a Value to a Name

##  Description

Assign a value to a name in an environment.

##  Usage

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

##  Arguments

x

a variable name, given as a character string. No coercion is done, and the
first element of a character vector of length greater than one will be used,
with a warning.

value

a value to be assigned to ` x ` .

pos

where to do the assignment. By default, assigns into the current environment.
See ‘Details’ for other possibilities.

envir

the ` [ environment ](/link/environment?package=base&version=3.6.2) ` to use.
See ‘Details’.

inherits

should the enclosing frames of the environment be inspected?

immediate

an ignored compatibility feature.

##  Value

This function is invoked for its side effect, which is assigning ` value ` to
the variable ` x ` . If no ` envir ` is specified, then the assignment takes
place in the currently active environment.

If ` inherits ` is ` TRUE ` , enclosing environments of the supplied
environment are searched until the variable ` x ` is encountered. The value is
then assigned in the environment in which the variable is encountered
(provided that the binding is not locked: see ` [ lockBinding
](/link/lockBinding?package=base&version=3.6.2) ` : if it is, an error is
signaled). If the symbol is not encountered then assignment takes place in the
user's workspace (the global environment).

If ` inherits ` is ` FALSE ` , assignment takes place in the initial frame of
` envir ` , unless an existing binding is locked or there is no existing
binding and the environment is locked (when an error is signaled).

##  Details

There are no restrictions on the name given as ` x ` : it can be a non-
syntactic name (see ` [ make.names
](/link/make.names?package=base&version=3.6.2) ` ).

The ` pos ` argument can specify the environment in which to assign the object
in any of several ways: as ` -1 ` (the default), as a positive integer (the
position in the ` [ search ](/link/search?package=base&version=3.6.2) ` list);
as the character string name of an element in the search list; or as an ` [
environment ](/link/environment?package=base&version=3.6.2) ` (including using
` [ sys.frame ](/link/sys.frame?package=base&version=3.6.2) ` to access the
currently active function calls). The ` envir ` argument is an alternative way
to specify an environment, but is primarily for back compatibility.

` assign ` does not dispatch assignment methods, so it cannot be used to set
elements of vectors, names, attributes, etc.

Note that assignment to an attached list or data frame changes the attached
copy and not the original object: see ` [ attach
](/link/attach?package=base&version=3.6.2) ` and ` [ with
](/link/with?package=base&version=3.6.2) ` .

##  References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_ .
Wadsworth & Brooks/Cole.

##  See Also

` [ <- ](/link/%26lt%3B-?package=base&version=3.6.2) ` , ` [ get
](/link/get?package=base&version=3.6.2) ` , the inverse of ` assign() ` , ` [
exists ](/link/exists?package=base&version=3.6.2) ` , ` [ environment
](/link/environment?package=base&version=3.6.2) ` .

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fassign&utm_source=r-docs&utm_medium=docs&utm_term=assign&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    for(i in 1:6) { #-- Create objects  'r.1', 'r.2', ... 'r.6' --
        nam <- paste("r", i, sep = ".")
        assign(nam, 1:i)
    }
    ls(pattern = "^r..$")
    
    ##-- Global assignment within a function:
    myf <- function(x) {
        innerf <- function(x)