EVOLUTION-MANAGER
Edit File: assign.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Assign a Value to a Name</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for assign {base}"><tr><td>assign {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Assign a Value to a Name</h2> <h3>Description</h3> <p>Assign a value to a name in an environment. </p> <h3>Usage</h3> <pre> assign(x, value, pos = -1, envir = as.environment(pos), inherits = FALSE, immediate = TRUE) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>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.</p> </td></tr> <tr valign="top"><td><code>value</code></td> <td> <p>a value to be assigned to <code>x</code>.</p> </td></tr> <tr valign="top"><td><code>pos</code></td> <td> <p>where to do the assignment. By default, assigns into the current environment. See ‘Details’ for other possibilities.</p> </td></tr> <tr valign="top"><td><code>envir</code></td> <td> <p>the <code><a href="environment.html">environment</a></code> to use. See ‘Details’.</p> </td></tr> <tr valign="top"><td><code>inherits</code></td> <td> <p>should the enclosing frames of the environment be inspected?</p> </td></tr> <tr valign="top"><td><code>immediate</code></td> <td> <p>an ignored compatibility feature.</p> </td></tr> </table> <h3>Details</h3> <p>There are no restrictions on the name given as <code>x</code>: it can be a non-syntactic name (see <code><a href="make.names.html">make.names</a></code>). </p> <p>The <code>pos</code> argument can specify the environment in which to assign the object in any of several ways: as <code>-1</code> (the default), as a positive integer (the position in the <code><a href="search.html">search</a></code> list); as the character string name of an element in the search list; or as an <code><a href="environment.html">environment</a></code> (including using <code><a href="sys.parent.html">sys.frame</a></code> to access the currently active function calls). The <code>envir</code> argument is an alternative way to specify an environment, but is primarily for back compatibility. </p> <p><code>assign</code> does not dispatch assignment methods, so it cannot be used to set elements of vectors, names, attributes, etc. </p> <p>Note that assignment to an attached list or data frame changes the attached copy and not the original object: see <code><a href="attach.html">attach</a></code> and <code><a href="with.html">with</a></code>. </p> <h3>Value</h3> <p>This function is invoked for its side effect, which is assigning <code>value</code> to the variable <code>x</code>. If no <code>envir</code> is specified, then the assignment takes place in the currently active environment. </p> <p>If <code>inherits</code> is <code>TRUE</code>, enclosing environments of the supplied environment are searched until the variable <code>x</code> 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><a href="bindenv.html">lockBinding</a></code>: 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). </p> <p>If <code>inherits</code> is <code>FALSE</code>, assignment takes place in the initial frame of <code>envir</code>, unless an existing binding is locked or there is no existing binding and the environment is locked (when an error is signaled). </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <h3>See Also</h3> <p><code><a href="assignOps.html"><-</a></code>, <code><a href="get.html">get</a></code>, the inverse of <code>assign()</code>, <code><a href="exists.html">exists</a></code>, <code><a href="environment.html">environment</a></code>. </p> <h3>Examples</h3> <pre> 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 </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>