tapply not simplifying to vector? (PR#320)

mcw@ln.nimh.nih.gov mcw@ln.nimh.nih.gov
Thu, 11 Nov 1999 04:56:34 +0100 (MET)

Hi, all.

The help file for tapply says that if simplify is true, and the result of
the calculation is always a scalar, then tapply will return a vector.


> t1 <- tapply(runif(10), rep(1:5, 2), mean)  
> is.vector(t1)  
[1] FALSE 
> is.array(t1) 
[1] TRUE

I have found this in version 0.65.1 on an SGI running Irix 6.5, and on a
Mac running Linux-PPC.  I've also seen it on an Intel machine running
RedHat 6.0.

I noticed this because some results that I expected to be vectors couldn't
be combined into a data frame (Error: can't coerce array into a

It seems that the check and conversion simply never happens.  One way to
fix this is to simply add these few lines to the end of the function (just
before the value "ansmat" is returned: 

  if(length(dim(ansmat)) == 1){
    dim(ansmat) <- NULL
    names(ansmat) <- namelist[[1]]

I wouldn't be surprised to find that there's a more elegant way to do
this.  Perhaps ansmat[index] should be returned instead of ansmat?  But I
haven't tested this extensively, so I don't know whether it might break
something else.

Hope this report helps.

Thanks for the terrific software.

Matt Wiener

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