From h.wickham at gmail.com  Tue Apr 10 19:33:34 2007
From: h.wickham at gmail.com (hadley wickham)
Date: Tue, 10 Apr 2007 12:33:34 -0500
Subject: [Rd] list/matrix chimera
In-Reply-To: <m28xd0xiei.fsf@fhcrc.org>
References: <m2lkh0xp4i.fsf@fhcrc.org> <461BA714.5040009@blackmesacapital.com>
	<f8e6ff050704100930p444beafwe2b6ed1caf229927@mail.gmail.com>
	<m28xd0xiei.fsf@fhcrc.org>
Message-ID: <f8e6ff050704101033v51a7b3d7w755babb636293024@mail.gmail.com>

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



[Rd] list/matrix chimera
hadley wickham