[R] within()

Duncan Murdoch murdoch.duncan at gmail.com
Thu May 19 16:38:21 CEST 2011


On 19/05/2011 9:32 AM, Timothy Bates wrote:
> The most interesting thing in this thread for me was "within()" - that is what I want 99% of the time, but I had only ever heard of with()
>
> A real boon! Thanks Bill V!
>
> The help for within gives this example for with(), which seems unnecessary, as glm supports data=
>
> with(anorexia, {
>      anorex.1<- glm(Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian)
>      summary(anorex.1)
> })
>
> especially when this is easier to read:
>    m<- glm(Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian, data=anorexia)
>    summary(m)
>
> The Rd file also has (just?) one example for within… I suggest deleting the anorexia example so the within example comes first, and perhaps supplementing with
>
> ## Not run:
> # More advanced example of usage (not run)
> model1<- glm.nb(Cells ~ Cryogel*Day, data = myData)
> myData2<- within(myData, Cryogel<- relevel(Cryogel, ref = "2"))
> model2<- update(model1, data = myData1)
> ## End(Not run)
>
>
> Also, the help page for transform does not back link to within… That would be helpful. It also uses an example with attach, which might encourage less good habits.
>
> Best wishes, and thanks,
> tim
> PS   : How does one best contribute to help files?

One way is to post suggestions to the R-devel list.  For the suggestion 
to add the See also link, that's probably best (but no need to do it 
now, I'll add it).  The problem with posting suggestions to R-help is 
that they are more likely to get lost.

For more extensive suggestions, it's best if you actually do the work 
and post a patch.  Coming up with good examples is hard, and so often 
the response to a suggestion like "the example should be better" is "I 
agree", rather than actually changing it.  But if you put together an 
example that is obviously better, it's easy to just paste it in, and so 
more likely to happen.

In this particular case, I don't agree that your change is an 
improvement.  "Not run" examples are bad, because they are rarely 
tested.  A more compelling use of with() would be a nice change to the 
help page, if you want to put one together.


> PPS: If R was to go to google summer of code, apart from helpful help, my wishlist would prioritise standardising all common functions to (where possible)
> 1. Have a formula interface
> 2. Support “data=“
> 3. Use a uniform na handling, with examples

R does have GSOC projects in the works.  A call for proposals was posted 
to the R-devel list back in February; you can read about it here:  
http://rwiki.sciviews.org/doku.php?id=developers:projects:gsoc2011, with 
links to current discussion.

> Diverse NA behavior catches most of our students out (now all three departments of our school using R exclusively at the PG level)
> NA behaviour can be
> na.rm=     as in rowMeans(na.rm=T))
> use=         as in an cor(use=“every”)   # with well documented effects in ?cor(), and all legal options  shown (everything|all.obs|complete.obs|na.or.complete|pairwise.complete.obs)
> na.action=  as in t.test(na.action=“na.omit”) # with no examples in ?t.test, nor a list of legal values
>
> Likewise it would help reduce confusion when
> plot(~Source, data=Df) # works
> # but
> boxplot(~Source, data=Df)
> # Error in boxplot.formula(~Source, data = Df) :
> #  'formula' missing or incorrect
>
> The formula isn’t missing or illformed, it’s that boxplot requires formulae to have something on the left hand side.


Tested fixes should be posted to R-devel.  Generally patches should be 
based on the R-devel (trunk) source, available at 
https://svn.r-project.org/R/trunk.

Duncan Murdoch



More information about the R-help mailing list