[Rd] replicating lists

Jonathan Rougier J.C.Rougier@durham.ac.uk
Thu, 12 Apr 2001 09:13:21 +0100 (BST)


Hi Brian,

After a discussion with David James can you clarify exactly what you think
the behaviour of "rep" will be?  The three alternatives for rep(fred, 5)
would appear to be

> fred <- list(happy = 1:10, name = "squash")
> list(fred)[rep(1, 5)]                          # version 1
> fred[rep(seq(along=fred), length=5)]           # version 2
> unlist(list(fred)[rep(1, 5)], recursive=FALSE) # version 3

I was expecting the first, as this makes sense in the context of

> tmp <- array(fred, c(3, 4))

but I can see that the second is also a natural way to think about
replicating a list.  I also notice that S+ uses version 3.  Personally I
don't like version 3 as it is going to duplicate names, and it is not
going to fit with "array".

Confused, Jonathan.

On Wed, 11 Apr 2001, Prof Brian Ripley wrote:

> The most obvious fix is internal, and that should be done fairly soon.
> 
> 
> On Wed, 11 Apr 2001, David James wrote:
> 
> > hi,
> >
> > Yes, I agree that the work-around needs some care, but I'm not sure
> > we need to wrap the list itself -- we're simply using properties of
> > "[" on lists.  If the subscript is rep(1, 10), then we'll get the
> > first element 10 times (which is *not* replicating the list).
> > To get the replication we need to index every element in
> > the list as many times as required.
> >
> > David
> >
> > > Date: Wed, 11 Apr 2001 15:28:02 +0100 (BST)
> > > From: Jonathan Rougier <J.C.Rougier@durham.ac.uk>
> > > X-Sender: dma0jcr@laplace
> > > To: David James <dj@research.bell-labs.com>
> > > cc: r-devel@stat.math.ethz.ch
> > > Subject: Re: [Rd] replicating lists
> > > MIME-Version: 1.0
> > >
> > > On Wed, 11 Apr 2001, David James wrote:
> > >
> > > > A workaround is to use rep() inside "[":
> > > >
> > > > > a <- list(a=1, b=2, c=3)
> > > > > a[rep(1:3, 1:3)]
> > > > $a
> > > > [1] 1
> > > >
> > > > $b
> > > > [1] 2
> > > >
> > > > $b
> > > > [1] 2
> > > >
> > > > $c
> > > > [1] 3
> > > >
> > > > $c
> > > > [1] 3
> > > >
> > > > $c
> > > > [1] 3
> > > >
> > > > Note, however, that names are no longer unique!
> > >
> > > This work-around is cunning but needs a bit of care, as to get the
> > > replication of the whole of fred you need to wrap the list itself:
> > >
> > > > fred <- list(happy = 1:10, fred = "squash")
> > > > list(fred)[rep(1, 10)] # list with 10 copies of fred
> > >
> > > Cheers, Jonathn.

Jonathan Rougier                       Science Laboratories
Department of Mathematical Sciences    South Road
University of Durham                   Durham DH1 3LE
tel: +44 (0)191 374 2361, fax: +44 (0)191 374 7388
http://www.maths.dur.ac.uk/stats/people/jcr/jcr.html

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._