What are the benefits of using with()
? In the help file it mentions it evaluates the expression in an environment it creates from the data. What are the benefits of this? Is it faster to create an environment and evaluate it in there as opposed to just evaluating it in the global environment? Or is there something else I'm missing?

- 32,245
- 6
- 36
- 77

- 2,736
- 1
- 15
- 31
-
1Mostly to save keystrokes when you have a long array name. But this is off-topic because many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. – Pierre L Feb 16 '17 at 20:03
-
@PierreLafortune I was wondering that too, in the examples though they have this `library(MASS) with(anorexia, { anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian) summary(anorex.1) })` which uses a lot more keystrokes to include the with statement. – Kristofersen Feb 16 '17 at 20:04
-
This might be a good topic for the [R-Public chat](http://chat.stackoverflow.com/rooms/25312/r-public). – Pierre L Feb 16 '17 at 20:06
-
@PierreLafortune Sure, I can do that. Should I remove the question? – Kristofersen Feb 16 '17 at 20:07
-
2Don't delete. The community will decide through votes and participation. – Pierre L Feb 16 '17 at 20:07
-
where did you find that example? The version in the main `?glm` page doesn't use `with()` (at least not in my version of R ...) I agree it's not worth it in that case. – Ben Bolker Feb 16 '17 at 20:11
-
1@BenBolker that was copied from the help files of with() – Kristofersen Feb 16 '17 at 20:12
-
1It's a good and worthwhile question which most R learners will eventually encounter, so let's all come up with a non-subjectie wording and keep it open. – smci Feb 16 '17 at 20:16
-
1It's a silly example. Maybe I'll try to get it changed to something better. – Ben Bolker Feb 16 '17 at 20:31
-
... the example I just suggested at r-devel@r-project.org was `with(mtcars,mpg[cyl==8 & disp>350])` – Ben Bolker Feb 16 '17 at 20:38
2 Answers
with
is a wrapper for functions with no data
argument
There are many functions that work on data frames and take a data
argument so that you don't need to retype the name of the data frame for every time you reference a column. lm
, plot.formula
, subset
, transform
are just a few examples.
with
is a general purpose wrapper to let you use any function as if it had a data argument.
Using the mtcars
data set, we could fit a model with or without using the data argument:
# this is obviously annoying
mod = lm(mtcars$mpg ~ mtcars$cyl + mtcars$disp + mtcars$wt)
# this is nicer
mod = lm(mpg ~ cyl + disp + wt, data = mtcars)
However, if (for some strange reason) we wanted to find the mean
of cyl + disp + wt
, there is a problem because mean
doesn't have a data argument like lm
does. This is the issue that with
addresses:
# without with(), we would be stuck here:
z = mean(mtcars$cyl + mtcars$disp + mtcars$wt)
# using with(), we can clean this up:
z = with(mtcars, mean(cyl + disp + wt))
Wrapping foo()
in with(data, foo(...))
lets us use any function foo
as if it had a data
argument - which is to say we can use unquoted column names, preventing repetitive data_name$column_name
or data_name[, "column_name"]
.
When to use with
Use with
whenever you like interactively (R console) and in R scripts to save typing and make your code clearer. The more frequently you would need to re-type your data frame name for a single command (and the longer your data frame name is!), the greater the benefit of using with
.
Also note that with
isn't limited to data frames. From ?with
:
For the default
with
method this may be an environment, a list, a data frame, or an integer as insys.call
.
I don't often work with environments, but when I do I find with
very handy.
When you need pieces of a result for one line only
As @Rich Scriven suggests in comments, with
can be very useful when you need to use the results of something like rle
. If you only need the results once, then his example with(rle(data), lengths[values > 1])
lets you use the rle(data)
results anonymously.
When to avoid with
When there is a data
argument
Many functions that have a data
argument use it for more than just easier syntax when you call it. Most modeling functions (like lm
), and many others too (ggplot
!) do a lot with the provided data
. If you use with
instead of a data
argument, you'll limit the features available to you. If there is a data
argument, use the data
argument, not with
.
Adding to the environment
In my example above, the result was assigned to the global environment (bar = with(...)
). To make an assignment inside the list/environment/data, you can use within
. (In the case of data.frames
, transform
is also good.)
In packages
Don't use with
in R packages. There is a warning in help(subset)
that could apply just about as well to with
:
Warning This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like
[
, and in particular the non-standard evaluation of argument subset can have unanticipated consequences.
If you build an R package using with
, when you check it you will probably get warnings or notes about using variables without a visible binding. This will make the package unacceptable by CRAN.
Alternatives to with
Don't use attach
Many (mostly dated) R tutorials use attach
to avoid re-typing data frame names by making columns accessible to the global environment. attach
is widely considered to be bad practice and should be avoided. One of the main dangers of attach is that data columns can become out of sync if they are modified individually. with
avoids this pitfall because it is invoked one expression at a time. There are many, many questions on Stack Overflow where new users are following an old tutorial and run in to problems because of attach
. The easy solution is always don't use attach
.
Using with
all the time seems too repetitive
If you are doing many steps of data manipulation, you may find yourself beginning every line of code with with(my_data, ...
. You might think this repetition is almost as bad as not using with
. Both the data.table
and dplyr
packages offer efficient data manipulation with non-repetitive syntax. I'd encourage you to learn to use one of them. Both have excellent documentation.

- 136,190
- 20
- 167
- 294
-
-
3It's also super useful when working with `rle`. You can access the values without having to assign the `rle`. Example: `with(rle(data), lengths[values > 1])`. I find it quite useful, anyhow. – Rich Scriven Feb 17 '17 at 17:03
I use it when i don't want to keep typing dataframe$
. For example
with(mtcars, plot(wt, qsec))
rather than
plot(mtcars$wt, mtcars$qsec)
The former looks up wt
and qsec
in the mtcars
data.frame. Of course
plot(qsec~wt, data = mtcars)
is more appropriate for plot or other functions that take a data=
argument.

- 136,190
- 20
- 167
- 294

- 195,160
- 17
- 277
- 295
-
-
1@Rentrop Can you please give us an example with `transform` and/or `within`? – Quazi Irfan Jul 16 '19 at 08:16