R-alpha: tapply() strangeness

Thomas Lumley thomas@biostat.washington.edu
Mon, 25 Aug 1997 09:31:20 -0700 (PDT)


On Mon, 25 Aug 1997, Kurt Hornik wrote:

> Here's something related to last week's apply() problem:
> 
> R> x <- matrix(1:20, nc = 4)
> R> x
>      [,1] [,2] [,3] [,4]
> [1,]    1    6   11   16
> [2,]    2    7   12   17
> [3,]    3    8   13   18
> [4,]    4    9   14   19
> [5,]    5   10   15   20
> 
> R> tapply(x, row(x), table)
> [1] Numeric,4 Numeric,4 Numeric,4 Numeric,4 Numeric,4
> 

Not terribly strange.  What we have here is a vector of lists (not a list
of lists as in S).  If you look at the components you see that everything
is the same as in S, it just prints differently
> tapply(x, row(x), table)->a
> a[1]
$1
 1  6 11 16 
 1  1  1  1 
or 
> a[[1]]
 1  6 11 16 
 1  1  1  1 

According to the Blue Book tapply() is supposed to return an array of
mode list  when the result of FUN applied to each category is not of
length 1. What you see is the result of my taking this literally: a
one-dimensional array
> dim(a)
[1] 5
> dimnames(a)
[[1]]
[1] "1" "2" "3" "4" "5"

I must confess that I didn't even know one-dimensional arrays were
possible -- I thought we would get a vector of mode list. Still, it seems
to work ok.

Thomas Lumley
------------------------------------------------------+------
Biostatistics		: "Never attribute to malice what  :
Uni of Washington	:  can be adequately explained by  :
Box 357232		:  incompetence" - Hanlon's Razor  :
Seattle WA 98195-7232	:				   :
------------------------------------------------------------

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel 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-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-