# with

0th

Percentile

##### Evaluate an Expression in a Data Environment

Evaluate an R expression in an environment constructed from data, possibly modifying (a copy of) the original data.

Keywords
programming, data
##### Usage
with(data, expr, …)
within(data, expr, …)
# S3 method for list
within(data, expr, keepAttrs = TRUE, …)
##### Arguments
data

data to use for constructing an environment. For the default with method this may be an environment, a list, a data frame, or an integer as in sys.call. For within, it can be a list or a data frame.

expr

expression to evaluate; particularly for within() often a “compound” expression, i.e., of the form

   {
a <- somefun()
b <- otherfun()
.....
rm(unused1, temp)
}

keepAttrs

for the list method of within(), a logical specifying if the resulting list should keep the attributes from data and have its names in the same order. Often this is unneeded as the result is a named list anyway, and then keepAttrs = FALSE is more efficient.

arguments to be passed to (future) methods.

##### Details

with is a generic function that evaluates expr in a local environment constructed from data. The environment has the caller's environment as its parent. This is useful for simplifying calls to modeling functions. (Note: if data is already an environment then this is used with its existing parent.)

Note that assignments within expr take place in the constructed environment and not in the user's workspace.

within is similar, except that it examines the environment after the evaluation of expr and makes the corresponding modifications to a copy of data (this may fail in the data frame case if objects are created which cannot be stored in a data frame), and returns it. within can be used as an alternative to transform.

##### Value

For with, the value of the evaluated expr. For within, the modified object.

##### Note

For interactive use this is very effective and nice to read. For programming however, i.e., in one's functions, more care is needed, and typically one should refrain from using with(), as, e.g., variables in data may accidentally override local variables, see the reference.

Further, when using modeling or graphics functions with an explicit data argument (and typically using formulas), it is typically preferred to use the data argument of that function rather than to use with(data, ...).

##### References

Thomas Lumley (2003) Standard nonstandard evaluation rules. http://developer.r-project.org/nonstandard-eval.pdf

evalq, attach, assign, transform.

##### Aliases
• with
• with.default
• within
• within.list
• within.data.frame