\HeaderA{assign}{Assign a Value to a Name}{assign}
\keyword{data}{assign}
\begin{Description}\relax
Assign a value to a name in an environment.
\end{Description}
\begin{Usage}
\begin{verbatim}
assign(x, value, pos = -1, envir = as.environment(pos),
       inherits = FALSE, immediate = TRUE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{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.
\item[\code{value}] a value to be assigned to \code{x}.
\item[\code{pos}] where to do the assignment.  By default, assigns into the
current environment.  See the details for other possibilities.
\item[\code{envir}] the \code{\LinkA{environment}{environment}} to use. See the details section.
\item[\code{inherits}] should the enclosing frames of the environment be
inspected?
\item[\code{immediate}] an ignored compatibility feature.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
There are no restrictions on \code{name}: it can be a non-syntactic
name (see \code{\LinkA{make.names}{make.names}}).

The \code{pos} argument can specify the  environment in which to assign
the object in any of several ways:
as an integer (the position in the \code{\LinkA{search}{search}} list); as
the character string name of an element in the search list; or as an
\code{\LinkA{environment}{environment}} (including using \code{\LinkA{sys.frame}{sys.frame}} to
access the currently active function calls).
The \code{envir} argument is an alternative way to specify an
environment, but is primarily there for back compatibility.

\code{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 \code{\LinkA{attach}{attach}}
and \code{\LinkA{with}{with}}.
\end{Details}
\begin{Value}
This function is invoked for its side effect, which is assigning
\code{value} to the variable \code{x}.  If no \code{envir} is
specified, then the assignment takes place in the currently active
environment.

If \code{inherits} is \code{TRUE}, enclosing environments of the supplied
environment are searched until the variable \code{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
\code{\LinkA{lockBinding}{lockBinding}}: 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 \code{inherits} is \code{FALSE}, assignment takes place in the
initial frame of \code{envir}, unless an existing binding is locked or
there is no existing binding and the environment is locked.
\end{Value}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{<\Rdash}{<.Rdash.}},
\code{\LinkA{get}{get}},
\code{\LinkA{exists}{exists}},
\code{\LinkA{environment}{environment}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
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) assign("Global.res", x^2, envir = .GlobalEnv)
 innerf(x+1)
}
myf(3)
Global.res # 16

a <- 1:4
assign("a[1]", 2)
a[1] == 2          #FALSE
get("a[1]") == 2   #TRUE

\end{ExampleCode}
\end{Examples}

