[R] "With" question

peter dalgaard pdalgd at gmail.com
Wed Sep 18 11:44:43 CEST 2013


On Sep 18, 2013, at 00:22 , Jeff Newmiller wrote:

> You seem to be applying the syntax for the within function to the with function. You should compare the documentation for them and choose your approach accordingly.

The syntax is fine, the difference in within() is that you can do assignments and that the return value is the modified data frame. 

However, the only way to print the value of each subexpression is to use explicit print(), or splitting it up as

with(data, sort(age))
with(data, length(age))
with(data, (age[10]+age[11])/2)

(This is why I'm a bit less dismissive towards attach() than some others are.) 

Of course, you can program your way out of anything in R (well almost anything), e.g.

> evalP <- function(e, ...) structure(lapply(e, eval,...), class="listofresults")
> print.listofresults <- function(x) for(i in x) print(i)
> dd <- data.frame(age=rbinom(20,100,.2))
> evalP(expression(sort(age), length(age), (age[10]+age[11])/2), dd)
 [1] 11 14 14 14 14 16 17 17 18 19 19 20 23 24 24 24 25 27 27 29
[1] 20
[1] 28

(which can be elaborated ad libitum)

-pd


> Also once inside a code block of any type, simply typing a variable name no longer prints it, so you need to use the appropriate function (print or cat).
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
>                                      Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> --------------------------------------------------------------------------- 
> Sent from my phone. Please excuse my brevity.
> 
> David Arnold <dwarnold45 at suddenlink.net> wrote:
>> All,
>> 
>> Trying to avoid using attach and detach while teaching class in Intro
>> Stats.
>> 
>> data=read.delim("dataset1.dat",header=TRUE)
>> with(data,{
>> sort(age)
>> length(age)
>> (age[10]+age[11])/2
>> })
>> 
>> However, this code only produces output for the last line between the
>> curly
>> braces. Granted, as we type the lines in one at a time and execute the
>> code,
>> we'll see each output. And, we can fix the problem by adding a print to
>> each
>> line.
>> 
>> Wonder if there are any other suggestions?
>> 
>> David
>> 
>> 
>> 
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/With-question-tp4676379.html
>> Sent from the R help mailing list archive at Nabble.com.
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list