[R] why doesn't table() have a data=argument?

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Fri Feb 20 16:38:31 CET 2009


On Fri, 20 Feb 2009, Michael Friendly wrote:

> In an Rweave tutorial written for possibly naive R users, I felt it necessary 
> to explain why table() had to be used inside with(), whereas other tools 
> like xtabs() had a data= argument.

I think the simple rule is: If there is a "formula", there is (typically) 
also a "data" argument, otherwise not.
There are probably exceptions but

So the question should probably be: Why is there xtabs() with interface
   xtabs(~ x + y, data = mydata)
instead of a "formula" method for table() that would allow
   table(~ x + y, data = mydata)

Well, table() is not generic, to some extent probably due to historical 
reasons. But maybe it would be worth considering a change to this?
Z

> with() is quite nice for such cases, but it seems an unnecessary thing to 
> learn right off.
> Before I turn this question into a request for R-devel, is there any inherent 
> reason why it might be
> hard to add a data= argument to table()?  I've looked at the code, but am not 
> enlightened
> on this question.
>
> \emph{Example}: Convert the \code{Arthritis} data in case form to a 3-way 
> table of
> \code{Treatment} $\times$ \code{Sex} $\times$ \code{Improved}.%
> \footnote{
> Unfortunately, \codefun{table} does not allow a \code{data} argument to 
> provide
> an environment in which the table variables are to be found.  In the
> examples in \secref{sec:table} I used \code{attach(mydata)} for this purpose,
> but \codefun{attach} leaves the variables in the global environment,
> while \codefun{with} just evaluates the \codefun{table} expression in a
> temporary environment of the data.
> }
> <<convert-ex2,results=verbatim>>=
> Art.tab <-with(Arthritis, table(Treatment, Sex, Improved))
> str(Art.tab)
> ftable(Art.tab)
> @
>
>
> -- 
> Michael Friendly     Email: friendly AT yorku DOT ca Professor, Psychology 
> Dept.
> York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
> 4700 Keele Street    http://www.math.yorku.ca/SCS/friendly.html
> Toronto, ONT  M3J 1P3 CANADA
>
> ______________________________________________
> 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.
>
>




More information about the R-help mailing list