[Rd] list/matrix chimera

hadley wickham h.wickham at gmail.com
Tue Apr 10 19:33:34 CEST 2007


On 4/10/07, Seth Falcon <sfalcon at fhcrc.org> wrote:
> "hadley wickham" <h.wickham at gmail.com> writes:
>
> > On 4/10/07, Tony Plate <tplate at blackmesacapital.com> wrote:
> >> Aren't you just seeing the effect of drop=TRUE? (at least with the
> >> examples you give below -- they all pick out a submatrix with extent one
> >> on some dimension)
> >>
> >> AFAICT, matrices with a list as the underlying data work properly,
>
> Yes, I guess so, although I'm surprised this is all that useful.  My
> mental model is that a matrix is a vector with dimension and that as
> such all elements are of the same type.  When a list is used for the
> underlying data, then this is still true in that each element is
> itself a list (at least according to vv[i]), but that is less useful
> -- even if you figure out to use [[, there is no guarantee what you
> will find there:
>
>     vv = c(list(a=list(inner=1), b="foo", c=matrix(1, nrow=2, ncol=2)),
>            as.list(1:7))
>     dim(vv) = c(2, 5)

That's rather a contrived example.  In practice, you would create a
list containing the same type of objects.  eg:

library(reshape)
models <- stamp(mtcars, vs ~ am, function(df) lm(mpg ~ wt, data=df))
models
dimnames(models)
summary(models[[1, 1]])

which I think is a valid and useful thing to do (although a little
contrived in this example)


> > I use this functionality a lot in the reshape package.  I would hate
> > to see it go!
> >
> > It would be nice to have some way to remove the [[1]] from vv[1,
> > 2][[1]] though.
>
> For lists, there is no guaranteed structure and so you can't avoid
> [[1]] since [1] will return a list and it can contain *anything*.

As (many) others have pointed out, vv[[1,2]] works, as I probably
should have checked!

> What could be useful is the notion of a vector of S4 objects.  There
> you could ensure they are all the same type.

Yes, but I know what I put in my structure.  The contract is not
enforced by code, but it is enforced by me.  Most of the things I'm
dealing with are S3 objects, so a S4 templated vector wouldn't be
particularly helpful.

Hadley



More information about the R-devel mailing list