[R] with and evaluation

Bert Gunter
Fri, 09 Sep 2016 00:29:12 -0000

I echo Duncan's plea.

But I can easily resolve one question:

"What's with "with?  It is one function I do not use because I
find it
incomprehensible. "

Consider:

## first, clear the workspace, also known as the Global
environment
> rm(list=ls())

## now create a data frame (or list or environment or...) containing
objects named "x" and "w"
> d <- data.frame(x=1:3,w=5:7)
 ## now define a different "x" in the
workspace
> x <- 4:6
>
> ## The following will produce an error, because there is no "w"
in the workspace
> ##
> w
Error: object 'w' not found
>
> ## But this won't, since with() tells it's expression to first
search in d.
>
> with(d,w)
[1] 5 6 7
>
> ## similarly
>
> ##error
> x+w
Error: object 'w' not found
>
> ## But
> with(d, x+w)
[1]  6  8 10
>
> ## In general, the second argument of d can be any expression that you
could type at the console.
>
> ## If something can't be found in d, it will be looked for in d's
"parent" environment, which is more involved than I want to get here.
But:
>
> y <- 5
>
> with(d, x+y) ## used x in d, and y in the workspace.
[1] 6 7 8

HTH

Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Sep 8, 2016 at 3:57 PM, Carl Sutton via R-help
<r-help at r-project.org> wrote:
> Hi
> I have doing the R-exercises to improve my R programming capabilities.
Data.frame exercise 4 showed me that I have  a language problem.  Here's the
problem and my "solution".
> #  Exercise 4#  Create a simple data frame from 3 vectors. Order the entire
data frame by the#  first column.df2 <- data.frame(a = 5:1,b = letters[1:5],
c = runif(5))order(df2$a) Naturally the order function did nothing.
> Per "help"Description
> order returns a permutation which rearranges its first argument into
ascending or descending order, breaking ties by further arguments. sort.list is
the same, using only one argument.See the examples for how to use these
functions to sort data frames, etc.
> Usage
> order(..., na.last = TRUE, decreasing = FALSE,      method =
c("shell", "radix"))
> sort.list(x, partial = NULL, na.last = TRUE, decreasing = FALSE,
method = c("shell", "quick", "radix"))Arguments
> ... a sequence of numeric, complex, character or logical vectors, all of
the same length, or a classed R object.
> Well, doesn't ... mean any legal object?  I gave it a legal object and
got nada.And the answer absolutely has me screaming "Say
What"df2[with(df2,order(a)),]
>
> What's with "with?  It is one function I do not use because I find
it incomprehensible.  To witEvaluate an R expression in an environment
constructed from data, possibly modifying (a copy of) the original data.
>
> First of all, if I'm not modifying data (or as a subset activity
creating data), why an I doing whatever it is I'm doing? ("possibly
modifying (a copy of) the original data.")
> Evaluate.  According to the thesarus A) assess(v), b) appraise, c) gage.
> OK, am I in a safe area?  I'll evaluate that.  Do I desire future
social contact with this person?  I'll evaluate that.In no way do I ever
evaluate an equation.  I may attempt to solve it.  I may do a computer program
to do the calculations and return a result.  I will probably evaluate the result
as to whether or not it helps solve the problem.  Think in terms of an income
tax return.  But evaluate an R expression?  No clue what that might mean.
> The remainder of the definition is also obtuse.  an R expression in an
environment constructed from data.  Why would one make an environment without
data?  Obviously I am missing the point.  My own created function makes a new
environment, but I only created it to crunch numbers.  If it doesn't crunch
numbers it's useless.
> The point is, I do not understand the definition of "with" and
thus have no idea how to use it.  I guess computerese is analogous to taxlawese.
Familiar words have entirely different meanings.
> Carl Sutton CPA
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.