[ 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)

#  with: Evaluate an Expression in a Data Environment

##  Description

Evaluate an  R  expression in an environment constructed from data, possibly
modifying (a copy of) the original 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 ](/link/list?package=base&version=3.6.2) ` method of `
within() ` , a ` [ logical ](/link/logical?package=base&version=3.6.2) `
specifying if the resulting list should keep the ` [ attributes
](/link/attributes?package=base&version=3.6.2) ` from ` data ` and have its `
[ names ](/link/names?package=base&version=3.6.2) ` 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.

##  Value

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

##  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 ` .

##  References

Thomas Lumley (2003) _Standard nonstandard evaluation rules_ . [
http://developer.r-project.org/nonstandard-eval.pdf
](http://developer.r-project.org/nonstandard-eval.pdf)

##  See Also

` [ evalq ](/link/evalq?package=base&version=3.6.2) ` , ` [ attach
](/link/attach?package=base&version=3.6.2) ` , ` [ assign
](/link/assign?package=base&version=3.6.2) ` , ` [ transform
](/link/transform?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%2Fwith&utm_source=r-docs&utm_medium=docs&utm_term=with&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    with(mtcars, mpg[cyl == 8  &  disp > 350])
        # is the same as, but nicer than
    mtcars$mpg[mtcars$cyl == 8  &  mtcars$disp > 350]
    
    require(stats); require(graphics)
    
    # examples from glm:
    with(data.frame(u = c(5,10,15,20,30,40,60,80,100),
                    lot1 = c(118,58,42,35,27,25,21,19,18),
                    lot2 = c(69,35,26,21,18,16,13,12,12)),
        list(summary(glm(lot1 ~ log(u), family = Gamma)),
             summary(glm(lot2 ~ log(u), family = Gamma))))
    
    aq <- within(airquality, {     # Notice that multiple vars can be changed
        lOzone <- log(Ozone)
        Month <- factor(month.abb[Month])
        cTemp <- round((Temp - 32) * 5/9, 1) # From Fahrenheit to Celsius
        S.cT <- Solar.R / cTemp  # using the newly created variable
        rm(Day, Temp)
    })
    head(aq)