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

Michael Friendly friendly at yorku.ca
Fri Feb 20 16:11:01 CET 2009


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.

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




More information about the R-help mailing list