Hi Everyone, At the moment it is not possible to replicate complex lists, but only simple ones:> rep(list(fred = 1:10), 10) # works fine > rep(list(fred = 1:10, happy = "squash"), 10)Error in rep(list(fred = 1:10, happy = "squash"), 10) : Unimplemented feature in rep There is nothing in ?rep that suggests that the latter should not work, and I think it would be a desirable feature if it did. For example, I want to create an array of lists, and it would be nice for me to set default values in the array function, rather than having to loop through the components and setting them individually. I've had a quick look in do_rep in seq.c and to my untrained eye it looks as though the LISTSXP case of the main switch might need to be replaced by a VECSXP, but I am not sure about other implications. Cheers, Jonathan. 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 11 Apr 2001, Jonathan Rougier wrote:> Hi Everyone, > > At the moment it is not possible to replicate complex lists, but only > simple ones: > > > rep(list(fred = 1:10), 10) # works fine > > rep(list(fred = 1:10, happy = "squash"), 10) > Error in rep(list(fred = 1:10, happy = "squash"), 10) : > Unimplemented feature in repIt is implemented in S3 and S4.> There is nothing in ?rep that suggests that the latter should not work, > and I think it would be a desirable feature if it did. For example, I > want to create an array of lists, and it would be nice for me to set > default values in the array function, rather than having to loop through > the components and setting them individually. > > I've had a quick look in do_rep in seq.c and to my untrained eye it looks > as though the LISTSXP case of the main switch might need to be replaced by > a VECSXP, but I am not sure about other implications.I suggest filing this as a bug report. -- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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! David A. James Statistics Research, Room 2C-253 Phone: (908) 582-3082 Bell Labs, Lucent Technologies Fax: (908) 582-3340 Murray Hill, NJ 09794-0636 ------------------------------------------------------------------------> X-Authentication-Warning: toucan.stats: ripley owned process doing -bs > Date: Wed, 11 Apr 2001 09:25:32 +0100 (BST) > From: Prof Brian Ripley <ripley@stats.ox.ac.uk> > X-X-Sender: <ripley@toucan.stats> > To: Jonathan Rougier <J.C.Rougier@durham.ac.uk> > cc: <r-devel@stat.math.ethz.ch> > Subject: Re: [Rd] replicating lists > MIME-Version: 1.0 > > On Wed, 11 Apr 2001, Jonathan Rougier wrote: > > > Hi Everyone, > > > > At the moment it is not possible to replicate complex lists, but only > > simple ones: > > > > > rep(list(fred = 1:10), 10) # works fine > > > rep(list(fred = 1:10, happy = "squash"), 10) > > Error in rep(list(fred = 1:10, happy = "squash"), 10) : > > Unimplemented feature in rep > > It is implemented in S3 and S4. > > > There is nothing in ?rep that suggests that the latter should not work, > > and I think it would be a desirable feature if it did. For example, I > > want to create an array of lists, and it would be nice for me to set > > default values in the array function, rather than having to loop through > > the components and setting them individually. > > > > I've had a quick look in do_rep in seq.c and to my untrained eye it looks > > as though the LISTSXP case of the main switch might need to be replaced by > > a VECSXP, but I am not sure about other implications. > > I suggest filing this as a bug report. > > -- > Brian D. Ripley, ripley@stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272860 (secr) > Oxford OX1 3TG, UK Fax: +44 1865 272595 > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-> 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 >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 fredCheers, 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 11 Apr 2001, David James wrote:> 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.Not sure I follow you here. I want n copies of an arbitrary list, as a list, so list(fred)[rep(1, n)] is desired, where fred is the list I want replicated. Unfortuntely this does not solve the underlying problem, which is that I want to do> tmp <- array(fred, c(3, 4))which breaks because "rep", invoked by "array", currently fails to handle complex lists. However I am happy to report that> tmp <- array(list(fred)[rep(1, 12)], c(3, 4))does work as expected:> tmp[[3, 4]] # fred again!So that's the work-around in my particular case. Cheers, Jonathan. 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._