Hi everyone I've been playing with do.call() but I'm having problems understanding it. I have a list of "n" elements, each one of which is "d" dimensional [actually an n-by-n-by ... by-n array]. Neither n nor d is known in advance. I want to bind the elements together in a higher-dimensional array. Toy example follows with d=n=3. f <- function(n){array(n,c(3,3,3))} x <- sapply(1:3,f,simplify=FALSE) Then what I want is ans <- abind(x[[1]] , x[[2]] , x[[3]] , along=4) [abind() is defined in library(abind)]. Note that dim(ans) is c(3,3,3,3), as required. PROBLEM: how do I do tell do.call() that I want to give abind() the extra argument along=4 (in general, I want along=length(dim(x[[1]]))+1)? Oblig Attempt: jj <- function(...){abind(... , along=4)} do.call("jj" , x) This works, because I know that d=3 (and therefore use along=4), but it doesn't generalize easily to arbitrary d. I'm clearly missing something basic. Anyone?
> do.call("abind" c(list.of.arrays, list(along=4)))This reminds me that I had been meaning to submit an enhancement of abind() that allows the first argument to be a list of arrays so that you could simply do abind(list.of.arrays, along=4), as I find this is a very common pattern. -- Tony Plate At Tuesday 04:48 PM 10/21/2003 +0100, Robin Hankin wrote:>Hi everyone > >I've been playing with do.call() but I'm having problems understanding it. > >I have a list of "n" elements, each one of which is "d" dimensional >[actually an n-by-n-by ... by-n array]. Neither n nor d is known in >advance. I want to bind the elements together in a higher-dimensional >array. > >Toy example follows with d=n=3. > >f <- function(n){array(n,c(3,3,3))} >x <- sapply(1:3,f,simplify=FALSE) > >Then what I want is > >ans <- abind(x[[1]] , x[[2]] , x[[3]] , along=4) > >[abind() is defined in library(abind)]. > >Note that dim(ans) is c(3,3,3,3), as required. > >PROBLEM: how do I do tell do.call() that I want to give abind() the >extra argument along=4 (in general, I want >along=length(dim(x[[1]]))+1)? > > >Oblig Attempt: > >jj <- function(...){abind(... , along=4)} >do.call("jj" , x) > >This works, because I know that d=3 (and therefore use along=4), but >it doesn't generalize easily to arbitrary d. I'm clearly missing >something basic. Anyone? > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help
I've also been thinking about how to specify that 'along' should be length(dim)+1. At the moment one can specify any number from 0 up to length(dim)+1, but as you point out you have to spell out length(dim)+1 as the value for the along argument. It would possible to make abind() automatically calculate along=length(dim)+1 when given along=NA, or along=-1, or along="+1". Any preferences? -- Tony Plate At Tuesday 04:48 PM 10/21/2003 +0100, Robin Hankin wrote:>Hi everyone > >I've been playing with do.call() but I'm having problems understanding it. > >I have a list of "n" elements, each one of which is "d" dimensional >[actually an n-by-n-by ... by-n array]. Neither n nor d is known in >advance. I want to bind the elements together in a higher-dimensional >array. > >Toy example follows with d=n=3. > >f <- function(n){array(n,c(3,3,3))} >x <- sapply(1:3,f,simplify=FALSE) > >Then what I want is > >ans <- abind(x[[1]] , x[[2]] , x[[3]] , along=4) > >[abind() is defined in library(abind)]. > >Note that dim(ans) is c(3,3,3,3), as required. > >PROBLEM: how do I do tell do.call() that I want to give abind() the >extra argument along=4 (in general, I want >along=length(dim(x[[1]]))+1)? > > >Oblig Attempt: > >jj <- function(...){abind(... , along=4)} >do.call("jj" , x) > >This works, because I know that d=3 (and therefore use along=4), but >it doesn't generalize easily to arbitrary d. I'm clearly missing >something basic. Anyone? > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help
I suggest following APL as that is a well thought out system. In APL terms there are two operations here called: - catenation. In abind, this occurs when along = 1,2,...,length(dim) - lamination. In abind, this occurs when along = length(dim) + 1 however, the latter is really only one case of lamination in which the added dimension comes at the end. To do it in full generality would require that one can add the new dimension at any spot including before the first, between the first and the second, ..., after the last. In APL notation, if along has a fractional part then the new dimension is placed between floor(along) and ceiling(along). Thus along=1.1 would put the new dimension between the first and second. The actual value of the fractional part is not material. --- From: Tony Plate <tplate at blackmesacapital.com> I've also been thinking about how to specify that 'along' should be length(dim)+1. At the moment one can specify any number from 0 up to length(dim)+1, but as you point out you have to spell out length(dim)+1 as the value for the along argument. It would possible to make abind() automatically calculate along=length(dim)+1 when given along=NA, or along=-1, or along="+1". Any preferences? -- Tony Plate _______________________________________________ No banners. No pop-ups. No kidding. Introducing My Way - http://www.myway.com
Please ignore this email. I just reread the abind documentation and it already does this! --- On Tue 10/21, Gabor Grothendieck < ggrothendieck at myway.com > wrote: From: Gabor Grothendieck [mailto: ggrothendieck at myway.com] To: tplate at blackmesacapital.com, rksh at soc.soton.ac.uk, r-help at stat.math.ethz.ch Date: Tue, 21 Oct 2003 13:22:28 -0400 (EDT) Subject: Re: [R] do.call() and aperm() <br><br>I suggest following APL as that is a well thought out system.<br>In APL terms there are two operations here called:<br><br>- catenation. In abind, this occurs when along = 1,2,...,length(dim)<br>- lamination. In abind, this occurs when along = length(dim) + 1<br><br>however, the latter is really only one case of lamination in <br>which the added dimension comes at the end. To do it in full<br>generality would require that one can add the new dimension<br>at any spot including before the first, between the first and<br>the second, ..., after the last.<br><br>In APL notation, if along has a fractional part then the new<br>dimension is placed between floor(along) and ceiling(along).<br>Thus along=1.1 would put the new dimension between the first<br>and second. The actual value of the fractional part is not material.<br><br>---<br>From: Tony Plate <tplate at blackmesacapital.com><br> <br>I've also been thinking about how to specify that 'along' should be <br>length(dim)+1. At the moment one can specify any number from 0 up to <br>length(dim)+1, but as you point out you have to spell out length(dim)+1 as <br>the value for the along argument. It would possible to make abind() <br>automatically calculate along=length(dim)+1 when given along=NA, or <br>along=-1, or along="+1". Any preferences?<br><br>-- Tony Plate<br><br><br><br>_______________________________________________<br>No banners. No pop-ups. No kidding.<br>Introducing My Way - http://www.myway.com<br><br>______________________________________________<br>R-help at stat.math.ethz.ch mailing list<br>https://www.stat.math.ethz.ch/mailman/listinfo/r-help<br> _______________________________________________ No banners. No pop-ups. No kidding. Introducing My Way - http://www.myway.com
On Tue, 21 Oct 2003, Robin Hankin wrote:> Hi everyone > > I've been playing with do.call() but I'm having problems understanding it. > > I have a list of "n" elements, each one of which is "d" dimensional > [actually an n-by-n-by ... by-n array]. Neither n nor d is known in > advance. I want to bind the elements together in a higher-dimensional > array. > > Toy example follows with d=n=3. > > f <- function(n){array(n,c(3,3,3))} > x <- sapply(1:3,f,simplify=FALSE) > > Then what I want is > > ans <- abind(x[[1]] , x[[2]] , x[[3]] , along=4) > > [abind() is defined in library(abind)]. > > Note that dim(ans) is c(3,3,3,3), as required. > > PROBLEM: how do I do tell do.call() that I want to give abind() the > extra argument along=4 (in general, I want > along=length(dim(x[[1]]))+1)? > > > Oblig Attempt: > > jj <- function(...){abind(... , along=4)} > do.call("jj" , x) > > This works, because I know that d=3 (and therefore use along=4), but > it doesn't generalize easily to arbitrary d. I'm clearly missing > something basic. Anyone? >If I have understood correctly, then d<-length(dim(x[[1]])) do.call("abind",c(x,along=d+1)) should work. -thomas