[R] Specification of factors in tapply

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Feb 21 18:38:22 CET 2001


On Wed, 21 Feb 2001, Hedderik van Rijn wrote:

>
> First of all, thanks for the helpful reply (also to Peter Dalgaard).
>
> Ripley:
>
> > I think you are just discovering that if you collapse an array to a
> > vector, you get the results in Fortran order.
>
> Dalgaard:
>
> > tapply() gives a *table*, in this case each element is a vector of
> > logicals. Tables are stored with first index varying the fastest. So
> > where's the surprise?
>
> Obviously, I overlooked the exact representation of the result of tapply.
> A nice example of conformation bias as the unlist command seemed the right
> function to use but clouded my the actual return structure. :-)
>
> If I understand correctly, my code depends on the "accidental" order of
> table representations. Browsing through of R scripts I wrote, it seems that
> I've been very lucky not to stumble upon this earlier as I use this
> "feature" quite often.
>
> So, two questions pop up.
>
> First, can I use this "feature" savely, or is should it be considered
> "undocumented" and instable? (I do not like the idea of possible different
> results if I ever happen to run the scripts using later versions of R.)

The `first index varying fastest' representation of an array is documented
and stable.

> Second, what are the functions/methods I could use to get these results in a
> more R-ish way? I quite often want to perform some function per subset of a
> dataframe and include the results back into the frame. As in the example I
> sent, finding the first value equal to or larger than 0 per
> subject/condition combination.

by() is a safer tool.  It gives you subsets labelled by the factors used
to generate them, and the results on those subsets.  It is a wrapper to
tapply, of course, and postdates some of the documentation.  (`An
Introduction to R' is still really Bill Venables' `Notes on S' from 1990.
We are well aware that it is not comprehensive, but are not attempting to
write R versions of existing S books, including two by Venables + ANO.)

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list