[R] force apply() to return a list

Gabor Grothendieck ggrothendieck at gmail.com
Mon Nov 21 15:11:21 CET 2005


On 11/21/05, Robin Hankin <r.hankin at noc.soton.ac.uk> wrote:
> Hi Gabor
>
> thanks for this.
>
>
> On 21 Nov 2005, at 13:26, Gabor Grothendieck wrote:
> > Try this:
> >
> > tapply(a, row(a), f, simplify = FALSE)
> >
>
> [
> here
> a <- cbind(1:5 , 5:1)
> f <- function(x){list(1:max(x[1],4))}
> ]
>
>
> This also works.  I actually  need unlist(... , recursive=FALSE)
> to pass the list to do.call() but that 's fine.
>
> But,  my question here is: how does one arrive at
> such a solution from the manpage?  The manpage seems
> to indicate that the first argument to tapply() is a ragged
> array, or a vector.
>
> I can't reconcile Gabor's  use of  tapply() with the manpage.
>
> How to  understand Gabor's suggestion better?

A matrix is just a vector with a dim attribute.

> x <- 1:4
> attr(x, "dim") <- c(2,2)  # or dim(x) <- c(2,2)
> class(x)
[1] "matrix"




More information about the R-help mailing list