alexru
give you a post
Description<br />
Assign a value to a name in an environment. <br />
<br />
Usage<br />
assign(x, value, pos = -1, envir = as.environment(pos),<br />
inherits = FALSE, immediate = TRUE)<br />
<br />
Arguments<br />
x a variable name (given as a quoted string in the function call). <br />
value a value to be assigned to x. <br />
pos where to do the assignment. By default, assigns into the current environment. See the details for other possibilities. <br />
envir the environment to use. See the details section. <br />
inherits should the enclosing frames of the environment be inspected? <br />
immediate an ignored compatibility feature. <br />
<br />
Details<br />
The pos argument can specify the environment in which to assign the object in any of several ways: as an integer (the position in the search list); as the character string name of an element in the search list; or as an environment (including using sys.frame to access the currently active function calls). The envir argument is an alternative way to specify an environment, but is primarily there for back compatibility. <br />
<br />
assign does not dispatch assignment methods, so it cannot be used to set elements of vectors, names, attributes, etc. <br />
<br />
Note that assignment to an attached list or data frame changes the attached copy and not the original object: see attach. <br />
<br />
Value<br />
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. <br />
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: 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). <br />
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.<br />
<br />
yihui
楼上帖的是R帮助,下面是S-Plus的:
assign S-PLUS Language Reference
Assign Object to Database or Frame
DESCRIPTION:
Assigns a value to a name. The location of the assignment can be a specific frame (useful when writing functions), a data directory, or a meta-database for functions or documentation.
USAGE:
assign(x, value, frame=<<see below>>, where=<<see below>>, immediate, meta)
REQUIRED ARGUMENTS:
x
character string giving the name of the object to be assigned.
value
any S-PLUS object; the value to be assigned the name in x.
OPTIONAL ARGUMENTS:
frame
number specifying in which of the frames of the current evaluation the object is to be assigned. frame=0 assigns to a database of objects (the session database) that will continue to be available throughout the current S-PLUS session, but will disappear at the end of the session. frame=1 means that the object is assigned to the expression frame. It will be available (from any frame) throughout the evaluation of the current expression, but will disappear when evaluation is complete. This is a useful way to create objects that are to be shared by several functions during the expression.
where
the database to which the object should be assigned. If where is supplied, it can either be a number or an object defining a database. A number implies the corresponding element of the search list, so where=2, for example, assigns an object to the second database. Using where=0 assigns to the session database. Otherwise, where is interpreted as a database, of any type. If the database is not currently attached, S-PLUS attaches it temporarily for the duration of the call. See attach for alternatives, usually more efficient, by which a database can be attached for the duration you need, without interfering with the search path.
Do not give both frame and where; if you do, S-PLUS reports an error.
immediate
a special option, meaningful only if where= is supplied and the database specified is the working data. In this case only, immediate=F causes assign to emulate the effect of an ordinary S-PLUS assignment expression in the top-level frame of the evaluator. That is, the object is assigned to frame 1, and copied at the end of the top-level task to the working data. Any other call to assign that includes where= performs the assignment immediately, regardless of this argument.
meta
if supplied, this specifies the purpose of the database to which the assignment takes place. For example, meta="help" says to assign to the help meta-data associated with the database where. This argument has no effect if frame is specified or where does not specify an attached chapter.
VALUE:
NULL.
SIDE EFFECTS:
assign is executed entirely for its side effect of assignment. Unlike the assignment operators, such as <-, assign does not return a non-NULL value.
The x argument provides the name of the object that will be created. For example, assign("a$b", 1) is distinctly not the way to create the component b of object a; rather it will create an object named a$b. If you created this object, you would need to use the get function to use the object. This allows you to create objects (to correspond with objects in another language, perhaps) whose names are not legal object names in S-PLUS. If you do want to assign the b component of a, then create all of a and assign a.
If both where and frame are omitted, the assign function works exactly the same way as the <- operator; that is, it assigns (permanently) to the working data when used at the top level (typed by the user, for example), and to the local frame of a function call when used from inside a function.
SEE ALSO:
Assignment , objcopy , remove , get , exists , attach , detach , search , synchronize , dbread .
EXAMPLES:
assign("abc", 1:3) # assign "abc"
assign(".Options", options, frame=0) # session dataset
assign(".Counts", counts, w=1) # to working data (even in a function)
# make up variable names in a loop
for(i in 1:10) assign(paste("sample", i, sep="."), runif(10))
# save each column of matrix x under a different name
for(i in seq(ncol(x)))
assign(colname, x[,i])